如何使用纯JavaScript切换两个跨度文本,不要使用jQuery。

huangapple go评论71阅读模式
英文:

How to toggle two span texts with pure Javascript, No jQuery please

问题

I am trying two toggle to spans text, but it does not switch or bring back from display none to display block.

function tog() {
  let fc = document.getElementById("fcol");
  let fe = document.getElementById("ferr");
  let pr = document.getElementById("pr");

  cdisp = fc.style.display;
  erdisp = fe.style.display;

  if (erdisp !== "block") {
    cdisp = "none";
    erdisp = "block";
    pr.innerHTML = fe.textContent;
  } else {
    erdisp = "none";
    cdisp = "block";
    pr.innerHTML = fc.textContent;
  }
}
body {
  text-align: center;
}
<span id="fcol" style="display: block; font-size:24px; color: green;">Only letters please</span>
<span id="ferr" style="display: none; font-size:24px; color: red;">Only letters please 2</span>
<button onClick="tog()">Sub</button>

<div id="pr"></div>
英文:

I am trying two toggle to spans text, but it does not switch or bring back from display none to display block.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function tog() {
  let fc = document.getElementById(&quot;fcol&quot;);
  let fe = document.getElementById(&quot;ferr&quot;);
  let pr = document.getElementById(&quot;pr&quot;);
  
  cdisp = fc.style.display;
  erdisp = fe.style.display;
  
  if (erdisp !== &quot;block&quot;) {
    cdisp = &quot;none&quot;;
    erdisp = &quot;block&quot;;	
    pr.innerHTML = fe.textContent;
  } else {
    erdisp = &quot;none&quot;;                                                                                                            
    cdisp = &quot;block&quot;;
    pr.innerHTML = fc.textContent;
  }
}

<!-- language: lang-css -->

body {
  text-align: center;
}

<!-- language: lang-html -->

&lt;span id = &quot;fcol&quot;  style=&quot;display: block; font-size:24px; color: green;&quot;&gt;Only letters please&lt;/span&gt; 
&lt;span id=&quot;ferr&quot;  style=&quot;display: none; font-size:24px; color: red;&quot;&gt;Only letters please 2&lt;/span&gt;
&lt;button onClick=&quot;tog()&quot;&gt;Sub&lt;/button&gt;

&lt;div id=&quot;pr&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 4

你应该创建一个可以切换的类。这将极大简化逻辑。

const
  fc = document.getElementById("fcol"),
  fe = document.getElementById("ferr"),
  pr = document.getElementById("pr");

function tog() {
  if (fe.classList.contains('hidden')) {
    pr.textContent = fe.textContent;
  } else {
    pr.textContent = fc.textContent;
  }
  
  // 切换类
  fc.classList.toggle('hidden');
  fe.classList.toggle('hidden');
}

pr.textContent = fe.textContent; // 设置初始文本
body {
  text-align: center;
  margin-top: 150px;
}

.hidden {
  display: none; /* 隐藏元素 */
}
<!-- 通过类使 fcol 隐藏 -->
<span id="fcol" class="hidden" style="font-size:24px; color:green;"> Only letters please </span>
<span id="ferr" style="font-size:24px; color:red;"> Only letters please 2</span>
<button onClick="tog()">Sub</button>
<div id="pr"></div>
英文:

You should create a class that you can toggle. This simplifies the logic greatly.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const
  fc = document.getElementById(&quot;fcol&quot;),
  fe = document.getElementById(&quot;ferr&quot;),
  pr = document.getElementById(&quot;pr&quot;);

function tog() {
  if (fe.classList.contains(&#39;hidden&#39;)) {
    pr.textContent = fe.textContent;
  } else {
    pr.textContent = fc.textContent;
  }
  
  // Toggle the classes
  fc.classList.toggle(&#39;hidden&#39;);
  fe.classList.toggle(&#39;hidden&#39;);
}

pr.textContent = fe.textContent; // Set initial text

<!-- language: lang-css -->

body {
  text-align: center;
  margin-top: 150px;
}

.hidden {
  display: none; /* hide element */
}

<!-- language: lang-html -->

&lt;!-- Make fcol hidden via the class --&gt;
&lt;span id=&quot;fcol&quot; class=&quot;hidden&quot; style=&quot;font-size:24px; color:green;&quot;&gt; Only letters please &lt;/span&gt;
&lt;span id=&quot;ferr&quot; style=&quot;font-size:24px; color:red;&quot;&gt; Only letters please 2&lt;/span&gt;
&lt;button onClick=&quot;tog()&quot;&gt;Sub&lt;/button&gt;
&lt;div id=&quot;pr&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

只是另一种处理这个问题的方式.. 你正在将 "block" 和 "none" 的字符串分配给变量 cdisperdisp。你没有分配整个 DOM 对象,所以你只是改变了变量,而不是对象的状态或样式。你仍然需要调用 DOM 对象本身。

所以,与其向你展示另一种方法来做这件事,这应该帮助你理解为什么你的代码一开始没有工作:

if (erdisp !== "block") {
    fc.style.display = "none";
    fe.style.display = "block";   
    pr.innerHTML = fe.textContent;
} else {
    fe.style.display = "none";
    fc.style.display = "block";
    pr.innerHTML = fc.textContent;
}
英文:

Just another way of speaking to this issue .. You are assigning the string of "block" and "none" to variables cdisp and erdisp. You are not assigning the entire DOM object, so you are simply changing the variable, not the state or style of the object. You still need to call on the DOM object itself.

So rather than show you another way to do it, this should help you understand why your code didn't work in the first place:

    if (erdisp !== &quot;block&quot;) {
        fc.style.display = &quot;none&quot;;
        fe.style.display = &quot;block&quot;;   
        pr.innerHTML = fe.textContent;
    } else {
        fe.style.display = &quot;none&quot;;
        fc.style.display = &quot;block&quot;;
        pr.innerHTML = fc.textContent;
    }

答案3

得分: 0

你的代码中遇到的问题是,你更新了cdisp和erdisp变量的值,但没有将这些更改反映到相应元素的样式中。

你可以通过将更新后的值赋给相应元素的style.display属性来更新代码。以下是更新后的代码:

function tog() {
    let fc = document.getElementById("fcol");
    let fe = document.getElementById("ferr");
    let pr = document.getElementById("pr");
        
    if (fe.style.display === "none") {
        fc.style.display = "none";
        fe.style.display = "block";   
        pr.innerHTML = fe.textContent;
    } else {
        fe.style.display = "none";                                                                                                            
        fc.style.display = "block";
        pr.innerHTML = fc.textContent;
    }
}

在这个版本中,if语句检查ferr元素是否被隐藏(none),如果是,则将其显示设置为block,并将fcol元素设置为none。然后相应地更新了pr元素的文本内容。else语句执行相反的操作,将fcol设置为block,将ferr设置为none。

<body>
    <div>
        <span id="fcol" style="display: block; font-size: 24px; color: green;"> Only letters please </span>
        <span id="ferr" style="display: none; font-size: 24px; color: red;"> Only letters please 2 </span>
        <button onClick="tog()">Sub</button>
    </div>
    <div id="pr"></div>
</body>

在这个HTML中,我删除了代码块外部的不相关内容,以使其更清晰。代码块内部的结构保持不变。

英文:

The issue you are facing in your code is that you are updating the values of the cdisp and erdisp variables but not reflecting these changes back to the styles of the respective elements.

You can update your code by assigning the updated values to the style.display properties of the respective elements. Here's the updated code:

function tog() {
    let fc = document.getElementById(&quot;fcol&quot;);
    let fe = document.getElementById(&quot;ferr&quot;);
    let pr = document.getElementById(&quot;pr&quot;);
        
    if (fe.style.display === &quot;none&quot;) {
        fc.style.display = &quot;none&quot;;
        fe.style.display = &quot;block&quot;;   
        pr.innerHTML = fe.textContent;
    } else {
        fe.style.display = &quot;none&quot;;                                                                                                            
        fc.style.display = &quot;block&quot;;
        pr.innerHTML = fc.textContent;
    }
}

In this version, the if statement checks if the ferr element is hidden (none), and if so, it sets its display to block, and sets the fcol element to none. The text content is then updated in the pr element accordingly. The else statement does the opposite, setting fcol to block and ferr to none.

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

function tog() {
let fc = document.getElementById(&quot;fcol&quot;);
let fe = document.getElementById(&quot;ferr&quot;);
let pr = document.getElementById(&quot;pr&quot;);
    
if (fe.style.display === &quot;none&quot;) {
    fc.style.display = &quot;none&quot;;
    fe.style.display = &quot;block&quot;;   
    pr.innerHTML = fe.textContent;
} else {
    fe.style.display = &quot;none&quot;;                                                                                                            
    fc.style.display = &quot;block&quot;;
    pr.innerHTML = fc.textContent;
}

}

<!-- language: lang-css -->

body {
            text-align: center;
            margin-top: 150px;
        }

<!-- language: lang-html -->

I am trying to toggle to spans text, but it does not switch or bring back the from display none to display block.

&lt;span id = &quot;fcol&quot;  style=&quot;display: block; font-size:24px; color:green;&quot;&gt; Only letters please &lt;/span&gt; 
    &lt;span id=&quot;ferr&quot;  style=&quot;display: none; font-size:24px; color:red;&quot; &gt; Only letters please 2 &lt;/span&gt;
    &lt;button onClick=&quot;tog()&quot;&gt;Sub&lt;/button&gt;
    
    &lt;div id=&quot;pr&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 01:05:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定