我对使用选择器并不确定如何在其他选择器中重用选择器,特别是当选择器链变长时如何在其他jQuery选择器中重用jquery选择器
$("#documents div div:last-child #element");
我写过jQuery代码。见here
HTML:
<button type="button" id="adddNewFile">Add</button>
<br>
<div id="documents"></div>
JQuery的:
$('#adddNewFile').click(function() {
$("#documents").append("<div>");
var d = $("#documents div:last-child");
d.append('File '+$("#documents div").length+': <input type="file" name="file" id="file"/>');
d.append('<button type="button" id="removeFile">Remove</button>');
d.append('<br/>');
$("#documents div:last-child #removeFile").click(function() {
$(this).parent().remove();
});
$('#documents').append(d);
});
如何解决上面的代码不能有多个jQuery的HTML元素试图像我现在做单独为:
$('#documents')
$("#documents div:last-child
$("#documents div:last-child #removeFile")
这不是最佳的性能。我如何纠正它?
只是一个供参考,如果该元素具有一个ID(其中** **必须是唯一的),刚刚那目标身份证,不需要走下祖先线。 – tymeJV
将$(“#documents div:last-child #removeFile”)更改为$(“#removeFile”)。另一个很好。 –