4
我得到了一个名为“nthchild”的变量var nthchild = ($(ui.selected).index() + 1);
这给了我一个列表项的第n个孩子与所选择的类。我什至登录到控制台,它工作正常。但是,当我尝试使用此变量,但它不起作用。参考错误:变量未定义
$("section:nth-child(" + nthchild + ")").style.marginTop = "200px";
因此,它应该给该部分200px的margin-top。但控制台是给我的错误
Uncaught ReferenceError: nthchild is not defined
你可以找到我的代码对这个codepen
$(function() {
$("#selectable").selectable();
});
$(function() {
$("#selectable").selectable({
selected: function(event, ui) {
var nthchild = ($(ui.selected).index() + 1);
console.log(nthchild);
}
});
});
$("section:nth-child(" + nthchild + ")").style.marginTop = "200px";
不应该是$(“section:nth-child(”+ nthchild +“)”)。css({'margin-top':'200px'});或document.getElementsByTagName(“section”)[nthchild] .style.marginTop =“200px”; – PersyJack
@PersyJack你是对的,谢谢。我错过了那部分 –
对。这很好用!但现在divs不会回到他们原来的位置,当我取消选择它们时:/ –