我的html页面中有四个类为col的article标记。在每篇文章中,我都有一个带有heading类的div。在里面我有一个img标签。
我试着遍历每一篇文章,去掉图像标签,并用一些不同的html替换。
在Wordpress中,这就是jquery函数包装器的原因。
现在我只是试图找到并替换工作(下面)-我还没有尝试每一篇文章的不同代码位。我不明白为什么下面的方法不起作用。
(function ($) {
$(document).ready(function() {
$('article.col').each(function(el) {
$((el).find('heading img').replaceWith('<newtag>somecode</newtag>'));
});
});
}(jQuery));发布于 2013-04-30 08:14:38
调用each时,函数值为function(index, element)。因此,现在,您正在尝试使用$(el)搜索一个数字,尝试使用$(this)或向您的函数添加其他参数。
此外,heading类也缺少点
$('article.col').each(function(index, el) {
$(el).find('.heading img').replaceWith('<newtag>somecode</newtag>');
});或
$('article.col').each(function() {
$(this).find('.heading img').replaceWith('<newtag>somecode</newtag>');
});发布于 2013-04-30 08:07:07
(function ($) {
$(document).ready(function() {
$('article.col').each(function(el) {
$(this).find('heading img').replaceWith('<newtag>somecode</newtag>');
});
});
}(jQuery));顺便说一句,jQuery包装器是为了防止与使用$符号的其他库发生冲突。
发布于 2013-04-30 08:13:10
语法有一些问题,
$(el.find('heading img').replaceWith('<newtag>somecode</newtag>')); 应该是
$(this).find('heading img').replaceWith('<newtag>somecode</newtag>'); 请看这个小提琴:
https://stackoverflow.com/questions/16290179
复制相似问题