简体   繁体   English

在函数中包含变量分配?

[英]Including variable assignments inside a function?

    <button onclick="insert()">Click to insert</button>
    <hr id="start">
    <hr id="end">

I wrote a javascript function that inserts a div between two horizontal rules: 我写了一个javascript函数,在两个水平规则之间插入div:

<script type="text/javascript">    
    var element = document.createElement("div");
    var element_content = document.createTextNode("This is a newly added row!");
    element.appendChild(element_content);

    var sibling = document.getElementById("end")     
    var parent = document.getElementById("start").parentNode;

    function insert(){
        parent.insertBefore(element,sibling);         
    }
</script>

However when I click the button for the second time, no divs are inserted. 但是,当我第二次单击该按钮时,没有插入div。 I had to include all the variable assignments inside the function in order to click on the button for the 2nd time have the div inserted: 我必须在函数中包含所有变量分配,以便第二次单击按钮时插入div:

<script type="text/javascript">
    function insert(){    
    var element = document.createElement("div");
    var element_content = document.createTextNode("This is a newly added row!");
    element.appendChild(element_content);

    var sibling = document.getElementById("end")     
    var parent = document.getElementById("start").parentNode;

        parent.insertBefore(element,sibling);         
    }
</script>

Can someone explain why my first approach didn't allow a 2nd div to be inserted after clicking the button? 有人可以解释一下为什么我的第一种方法不允许在单击按钮后插入第二个div吗?

In the first example you create one element, since you define it outside of the function. 在第一个示例中,您创建了一个元素,因为您在函数外部定义了它。 The first time the function is called, that element is appended to the DOM and it won't be appended again (it actually gets removed and appended again, but in the same place): 第一次调用该函数时,该元素将附加到DOM,并且不会再次附加(实际上它已被删除并再次附加,但在同一位置):

If the node already exists it is removed from current parent node, then added to new parent node. 如果该节点已经存在,则将其从当前父节点中删除,然后添加到新的父节点中。

See appendChild on MDN. 请参阅MDN上的appendChild

The second example creates a new element every time you call the function, and appends that new element to the DOM. 第二个示例在每次调用函数创建一个新元素,并将该新元素附加到DOM。

Of course! 当然! If you keep the element outside of the function, only 1 new div is created and you are inserting the same div over and over. 如果将元素保留在函数之外,则只会创建1个新的div,并且要一遍又一遍地插入相同的div。 When a div is inserted in a new location, it is removed from it's old location (if any). 将div插入新位置时,会将其从其旧位置删除(如果有)。

Your second method creates new divs every time the button is clicked. 每次单击按钮时,第二种方法都会创建新的div。

Because you're only creating a div once and then inserting it when you click the button. 因为您只创建一个div ,然后在单击按钮时将其插入。 You never create a second div , so how can you expect one to magically appear out of nowhere? 您永远不会创建第二个div ,那么如何期望一个div神奇地出现呢?

The second function works because it creates the div when you click the button, then inserts it. 第二个功能起作用是因为它在您单击按钮时创建了div ,然后将其插入。 Here you create one div for every click, so it works. 在这里,您为每次点击创建一个div ,因此可以正常工作。

在第一种方法中,您在函数外部声明的变量是在触发点击之前创建的,因此它们将始终以相同的状态存在,而与您的点击无关。

The first approach only creates the div once and inserts it in the DOM. 第一种方法仅创建一次div并将其插入到DOM中。 Calling the function twice doesn't create the div again like the second method does. 两次调用该函数不会像第二个方法那样再次创建div。 You could probably take the: 您可能会采取:

var sibling = document.getElementById("end")     
var parent = document.getElementById("start").parentNode;

out of the function though. 虽然功能不对。

The Script is syntactically correct and does what you tell it to do. 该脚本在语法上是正确的,并且可以执行您要求的操作。 You're inserting the same node to the same position, which results in... nothing. 您要将相同的节点插入相同的位置,这将导致...一切。

Try cloning the node: 尝试克隆节点:

function insert(){
    parent.insertBefore(element.cloneNode(true), sibling);         
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM