[英]Global variable not recognized after remove and append of an element
I have a global variable var $test = $('#test');
我有一个全局变量var $test = $('#test');
where #test
id of div that initially exist in DOM. 其中DOM中最初存在的div的#test
ID。 If i remove the div with remove()
and append it again then the global variable $test no longer recognizes the the newly appended div with id #test
. 如果我使用remove()
删除div并再次追加它,则全局变量$ test将不再识别ID为#test
的新追加的div。 What am i doing wrong. 我究竟做错了什么。 Why does this happen? 为什么会这样?
Check http://jsfiddle.net/mr3C4/ 检查http://jsfiddle.net/mr3C4/
Instead of creating a new element from the string, just append the original element: 无需从字符串创建新元素,只需追加原始元素即可:
var $test = $('#test');
$test.live('click', function() {
$test.toggleClass('blue'); // 2. continues to reference $test
});
$('button').click(function() {
$test.remove();
$test.appendTo('body'); // <-- 1. append the same element that you removed
});
Example: http://jsfiddle.net/mr3C4/5/ 示例: http : //jsfiddle.net/mr3C4/5/
That way your handler is still referencing the same element you have referenced in the $test
variable. 这样,您的处理程序仍将引用与$test
变量中引用的元素相同的元素。
This is because $test
is referencing a specific jQuery object that references a specific element, which is no longer part of the DOM. 这是因为$test
引用了一个特定的jQuery对象,该对象引用了一个特定的元素,该元素不再是DOM的一部分。 You were recreating an idential element from a string in your original code. 您正在使用原始代码中的字符串重新创建标识元素。
...or you can use the detach()
[docs] method instead of the remove()
[docs] method if you want to keep jQuery data associated with it. ...或者,如果您想使jQuery数据保持关联,则可以使用detach()
[docs]方法而不是remove()
[docs]方法。
var $test = $('#test');
$test.live('click', function() {
$test.toggleClass('blue');
});
$('button').click(function() {
$test.detach();
$test.appendTo('body');
});
Example: http://jsfiddle.net/mr3C4/6/ 示例: http : //jsfiddle.net/mr3C4/6/
EDIT: 编辑:
If you want to update your element, it is better to use the method above, and make modifications to the original. 如果要更新元素,最好使用上面的方法,并对原始方法进行修改。
If you can't do that because you're making a change that can't be made to the original, like changing the tagName
, then create your new element, and update your variable. 如果您因为要进行的更改而无法对原始内容进行更改(例如更改tagName
,那么请创建新元素并更新变量。
var $test = $('#test'),
x = '<p id="test"></p>';
$test.live('click', function() {
$test.toggleClass('blue');
});
$('button').click(function() {
$test.detach();
$test = $( x ).appendTo('body'); // update the variable
});
You could even keep the same jQuery object, and just update the element: 您甚至可以保留相同的jQuery对象,然后更新元素:
var $test = $('#test'),
x = '<p id="test"></p>';
$test.live('click', function() {
$test.toggleClass('blue');
});
$('button').click(function() {
$test.detach();
$test[0] = $( x ).appendTo('body')[0]; // update the element at index 0
});
Here you go: 干得好:
var $test = $('#test'),
str = '<div id="test"></div>';
$test.live('click', function() {
$(this).toggleClass('blue'); // here is the change, use $(this)
});
$('button').click(function() {
$test.remove();
$(str).appendTo('body');
});
Live demo: http://jsfiddle.net/mr3C4/3/ 现场演示: http : //jsfiddle.net/mr3C4/3/
Although your code has another issue: If you intend to replace the #test
element with a new one, you should also update the $test
object because otherwise it refers to an DIV element that doesn't exist anymore which isn't useful. 尽管您的代码还有另一个问题:如果打算将#test
元素替换为#test
元素,则还应该更新$test
对象,因为否则它引用的DIV元素不再存在,将不再有用。
Why does this happen? 为什么会这样?
Because your original variable, $test, only ever refers to the div with id=test that was in the DOM when you declared the variable. 因为您的原始变量$ test仅在声明该变量时引用id = test在DOM中的div。 Creating another element, even one with the same ID, does make it the same element you selected. 创建另一个元素,即使是具有相同ID的元素,也会使其与您选择的元素相同。
This slide contains another example you might find helpful. 此幻灯片包含另一个示例,您可能会发现有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.