首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript,循环循环

JavaScript,循环循环
EN

Stack Overflow用户
提问于 2016-06-08 11:31:12
回答 4查看 180关注 0票数 1

我有个问题..。如何让这两个文本字段分别显示放入其中的内容?

代码语言:javascript
复制
<input type="text" id="userInput"></input> <button id="submitter">Submit</button>
<div id="output" style="white-space: pre-wrap; display: inline;"></div>

<input type="text" id="userInput"></input> <button id="submitter">Submit</button>
<div id="output" style="white-space: pre-wrap; display: inline;"></div>

<script type="text/javascript">
        var didClickIt = false;
        document.getElementById("submitter").addEventListener("click",function(){
            // same as onclick, keeps the JS and HTML separate
            didClickIt = true;
        });

        setInterval(function(){
            // this is the closest you get to an infinite loop in JavaScript
            if( didClickIt ) {
                didClickIt = false;
                // document.write causes silly problems, do this instead (or better yet, use a library like jQuery to do this stuff for you)
                var o=document.getElementById("output"),v=document.getElementById("userInput").value;
                if(o.textContent!==undefined){
                    o.textContent=v;
                }else{
                    o.innerText=v;
                }
             }
         },500);
</script>

,我的问题是,当输入一个值时,其中一个框不能工作。我该怎么解决这个问题?

EN

回答 4

Stack Overflow用户

发布于 2016-06-08 11:40:25

不能在页面上重复id,您将得到一个无效的文档,而该文档的行为方式与您所期望的不同。

相反,给元素一个公共类并使用document.querySelectorAll查找它们,然后将两个元素上的事件挂钩,请参见注释:

代码语言:javascript
复制
// Get all buttons
var buttons = document.querySelectorAll(".submitter");

// Loop through them setting up handlers
Array.prototype.forEach.call(
  buttons,
  function(button, index) {
    button.addEventListener("click", function(e) {
      // Call our handler, telling it which button was clicked
      return handleClick(e, this, index);
    }, false);
  }
);

// Handle a click
function handleClick(e, button, index) {
  // Get the userInput and output elements with the same index
  var userInput = document.querySelectorAll(".userInput")[index];
  var output = document.querySelectorAll(".output")[index];
  if (userInput && output) {
    // Display the output
    output.innerHTML = "";
    output.appendChild(
      document.createTextNode(userInput.value)
    );
  }
}
代码语言:javascript
复制
<input type="text" class="userInput">
<button class="submitter">Submit</button>
<div class="output" style="white-space: pre-wrap; display: inline;"></div>
<input type="text" class="userInput">
<button class="submitter">Submit</button>
<div class="output" style="white-space: pre-wrap; display: inline;"></div>

请参阅this answer的“类似数组”部分,以了解奇怪的Array.prototype.forEach.call调用。

话虽如此,我想我可能会稍微修改HTML以使这更容易,但是在userInputsubmitteroutput元素的每个三位一体周围都会放置一个包装器元素:

代码语言:javascript
复制
// Get all buttons
var buttons = document.querySelectorAll(".submitter");

// Loop through them setting up handlers
Array.prototype.forEach.call(
  buttons,
  function(button) {
    button.addEventListener("click", handleClick, false);
  }
);

// Handle a click
function handleClick(e) {
  // Get the userInput and output elements that are in the same
  // container element
  var parent = this.parentNode;
  var userInput = parent.querySelector(".userInput");
  var output = parent.querySelector(".output");
  if (userInput && output) {
    // Display the output
    output.innerHTML = "";
    output.appendChild(
      document.createTextNode(userInput.value)
    );
  }
}
代码语言:javascript
复制
<div><!-- This is the wrapper -->
  <input type="text" class="userInput">
  <button class="submitter">Submit</button>
  <div class="output" style="white-space: pre-wrap; display: inline;"></div>
</div>
<div><!-- This is the wrapper -->
  <input type="text" class="userInput">
  <button class="submitter">Submit</button>
  <div class="output" style="white-space: pre-wrap; display: inline;"></div>
</div>

票数 1
EN

Stack Overflow用户

发布于 2016-06-08 11:34:09

两个输入具有相同的id,您必须更改其中一个并将侦听器添加到另一个输入。

票数 0
EN

Stack Overflow用户

发布于 2016-06-08 11:37:27

对于文本字段和按钮,您应该有两个不同的ids,并将单击事件添加到它们中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37701303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档