Javascript Jquery子元素的子元素
但是,在选择子元素之后,我无法选择更多的元素。我可以修改您的代码使其正常工作,但您执行此操作的方式有点不正常–即,在复制现有元素之前使用克隆来复制它 如果您要像DOM中的数组一样表示JavaScript数据结构,并希望保持它们的同步,那么最好使用模板系统。因为您使用的是jQuery,所以它看起来很不错。通过浏览自述文件,您的内容将变得如此简单:Javascript Jquery子元素的子元素,javascript,jquery,Javascript,Jquery,但是,在选择子元素之后,我无法选择更多的元素。我可以修改您的代码使其正常工作,但您执行此操作的方式有点不正常–即,在复制现有元素之前使用克隆来复制它 如果您要像DOM中的数组一样表示JavaScript数据结构,并希望保持它们的同步,那么最好使用模板系统。因为您使用的是jQuery,所以它看起来很不错。通过浏览自述文件,您的内容将变得如此简单: function addOption(that) { $(that).parent().parent().after($('.choice_
function addOption(that) {
$(that).parent().parent().after($('.choice_class_sample .choice_class').clone());
var parentQuestion = $(that).parent().parent().parent();
var lastChoice = parentQuestion.children('.choice_class:last');
$(parentQuestion.children('.choice_class:last-child .choice-no')).html(parentQuestion.children('.choice_class').length);
$(that).remove();
}
干净多了。最重要的是,它避免了在JS中进行丑陋的、强制性的DOM操作。我可以修改您的代码使其正常工作,但您执行此操作的方式有点非正统–即,在复制现有元素之前使用克隆来复制它 如果您要像DOM中的数组一样表示JavaScript数据结构,并希望保持它们的同步,那么最好使用模板系统。因为您使用的是jQuery,所以它看起来很不错。通过浏览自述文件,您的内容将变得如此简单:
function addOption(that) {
$(that).parent().parent().after($('.choice_class_sample .choice_class').clone());
var parentQuestion = $(that).parent().parent().parent();
var lastChoice = parentQuestion.children('.choice_class:last');
$(parentQuestion.children('.choice_class:last-child .choice-no')).html(parentQuestion.children('.choice_class').length);
$(that).remove();
}
干净多了。最重要的是,它避免了在JS中进行丑陋的、强制性的DOM操作。谢谢你的建议。它看起来干净整洁。我必须提到我有不止一个问题。每个问题都有自己的选择。对于我的编码,我使用find修复了这个问题;但我正按照你的建议改变它们。谢谢你的建议。它看起来干净整洁。我必须提到我有不止一个问题。每个问题都有自己的选择。对于我的编码,我使用find修复了这个问题;但我正按照你的建议改变它们。谢谢
<script type="text/html" id="template">
<div>
<div>
<label>
<span>Option <span data-content="id"></span></span>
<span class="required">*</span>
</label>
<div>
<input type="text" required="required">
</div>
<a href="#" class="add-choice-button"><span class="glyphicon glyphicon-plus"></span> Add Choice</a>
</div>
</div>
</script>
var options = [ { id: 1 } ]
parentQuestion.loadTemplate("#template", questions)
$('.add-choice-button').on('click', function () {
options.push({
id: options.length + 1
})
parentQuestion.loadTemplate('#template', options)
})