我看过:How to set margin with jquery?
这就是我正在尝试的:
$(document).ready(function () {
$(".content").each(function (el) {
el.css('margin', '0px');
})
});
虽然这不起作用;现在我必须在样式表中将边距设置为零。
最佳答案
css
已经遍历了每个元素:
$('.content').css('margin', '0');
您遇到的问题是 each
将 dom 节点本身传递给函数,但它是第二个参数:
$('.content').each(function (i, ele) {
$(ele).css('margin', '0');
//the context is also set to the element
$(this).css('margin', '0');
});
重新阅读你的问题后,看起来你选择了错误的选择器,我不知道你是想要所有的 child 还是所有的后代,选择器应该是:
$('.content > *')
-或-
$('.content *')
分别
关于javascript - 如何使用 jquery 为 div 中的每个元素设置边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10641273/