将两个类似的jQuery脚本组合成一个if/then脚本
我试图结合两个jQuery循环,基于回答问题的代码,从WordPress帖子中删除图像和iframe,并将它们移动到新div或新列表中。我以前的问题试图独立地实现这些结果中的每一个,但我现在需要将它们组合在一个页面中,根据父容器选择要使用的结果 具体地说,我希望第一个脚本在所有将两个类似的jQuery脚本组合成一个if/then脚本,jquery,wordpress,loops,jquery-selectors,posts,Jquery,Wordpress,Loops,Jquery Selectors,Posts,我试图结合两个jQuery循环,基于回答问题的代码,从WordPress帖子中删除图像和iframe,并将它们移动到新div或新列表中。我以前的问题试图独立地实现这些结果中的每一个,但我现在需要将它们组合在一个页面中,根据父容器选择要使用的结果 具体地说,我希望第一个脚本在所有容器上运行,除了带有#clientsID的容器。应该使用第二个脚本 我想我可能需要某种if/then逻辑来运行适合选择器的代码,但我很难实现这一点。目前,一个脚本正在破坏另一个脚本 以下是两个脚本-请注意第4行括号中的注释
容器上运行,除了带有#clients
ID的容器。应该使用第二个脚本
我想我可能需要某种if/then逻辑来运行适合选择器的代码,但我很难实现这一点。目前,一个脚本正在破坏另一个脚本
以下是两个脚本-请注意第4行括号中的注释:
jQuery("document").ready (function($){
// Elements you want to match
var matchesEl = "img, iframe"
// For each section [I have added a 'not-#clients' selector, which doesn't work but gives an idea of the logic I'm attempting], get its index number and do the following:
$('section !#clients').each(function(index){
//If there is a matched element (for each section),
if ($(this).find(matchesEl).length > 0) {
// create a div.royalSlider and prepend to this section
$('<div>', {'class': 'royalSlider'}).prependTo(this)
// For each matched element into this section (we don't need to get index number here),
$(this).find(matchesEl).each(function() {
// add .rsImg class to this element and insert on the div.royalSlider,
// not any div.royaSlider, but that one with equivalent index given before
$(this).addClass('rsImg').appendTo($('section .royalSlider').eq(index));
});
}
});
});
jQuery("document").ready (function($){
var matchesEl = "img, iframe"
$('section #clients').each(function(index){
if ($(this).find(matchesEl).length > 0) {
$('<ul>').appendTo($('section');
$(this).find(matchesEl).each(function() {
//The line code under will only works if all your <section>s always have at least one matched element.
//$("<li>").append($(this)).appendTo($('section ul').eq(index));
//So the right code is:
$("<li>").append($(this)).appendTo($('section').eq(index).children('ul'));
});
}
$('p').filter(function () { return $.trim(this.innerHTML) == "" }).remove();
});
});
jQuery(“文档”).ready(函数($){
//要匹配的元素
var matchesEl=“img,iframe”
//对于每个部分[我添加了一个'not-#clients'选择器,它不起作用,但给出了我正在尝试的逻辑的想法],获取其索引号并执行以下操作:
$('section!#clients')。每个(函数(索引){
//如果有匹配的元素(每个部分),
if($(this).find(matchesEl).length>0){
//创建一个div.royalSlider并在该部分前面加上前缀
$('',{'class':'royalSlider'}).prependTo(this)
//对于本节中的每个匹配元素(我们不需要在此处获取索引号),
$(this).find(matchesEl).each(function(){
//将.rsImg类添加到此元素并插入div.royalmslider,
//不是任何div.royaSlider,而是之前给出的具有等效索引的div.royaSlider
$(this.addClass('rsImg').appendTo($('section.royalsloider').eq(index));
});
}
});
});
jQuery(“文档”).ready(函数($){
var matchesEl=“img,iframe”
$('section#clients')。每个(函数(索引){
if($(this).find(matchesEl).length>0){
$(“”)。附录($('section');
$(this).find(matchesEl).each(function(){
//只有当您的所有s始终至少有一个匹配元素时,下面的行代码才会起作用。
//$(“- ”).append($(this)).appendTo($('section ul').eq(index));
//因此,正确的代码是:
$(“
- ”).append($(this)).appendTo($('section').eq(index.children('ul'));
});
}
$('p').filter(函数(){return$.trim(this.innerHTML)==“”}.remove();
});
});
我需要开发一个基于jQuery选择器有选择地运行的脚本,而不是运行两个冲突的脚本
我将尝试编写这个统一的脚本并将其发布在这里,而不是展示这两个脚本,但目前第二个脚本(尝试创建
元素)根本不起作用,我不确定如何单独工作,这似乎是将它们结合起来的先决条件
如果有帮助的话,我正在工作的网站可以被浏览(在开发阶段!)
非常感谢您在此方面提供的帮助。1)使用$('section:not(#clients)
而不是$('section!#clients')
2) 您不需要有jQuery(“document”).ready(函数($){…})代码>两次。您可以将所有脚本放在一个脚本中。如果varmatchesEl
第二次仍然具有相同的内容,则无需声明两次
3) 从语义上讲,如果使用名为clients
的id,则页面上必须只有一个#clients
,因此该元素中不需要each()
函数
上面的代码应该可以做到这一点:
jQuery("document").ready (function($){
// Elements you want to match
var matchesEl = "img, iframe"
// For each section [I have added a 'not-#clients' selector, which doesn't work but gives an idea of the logic I'm attempting], get its index number and do the following:
$('section:not(#clients)').each(function(index){
//If there is a matched element (for each section),
if ($(this).find(matchesEl).length > 0) {
// create a div.royalSlider and prepend to this section
$('<div>', {'class': 'royalSlider'}).prependTo(this)
// For each matched element into this section (we don't need to get index number here),
$(this).find(matchesEl).each(function() {
// add .rsImg class to this element and insert on the div.royalSlider,
// not any div.royaSlider, but that one with equivalent index given before
$(this).addClass('rsImg').appendTo($('section .royalSlider').eq(index));
});
}
});
//No need for each() here (neither index)
// If there is a matched element in #clients, do:
if ($('#clients').find(matchesEl).length > 0) {
// Append <ul> to #clients
$('<ul>').appendTo($('#clients'));
// For each matched element into #clients
$('#clients').find(matchesEl).each(function() {
//Create a <li>, append the element content into <li> and insert it on <ul> into #clients
$("<li>").append($(this)).appendTo($('#clients').children('ul'));
});
}
$('p').filter(function () { return $.trim(this.innerHTML) == "" }).remove();
});
jQuery(“文档”).ready(函数($){
//要匹配的元素
var matchesEl=“img,iframe”
//对于每个部分[我添加了一个'not-#clients'选择器,它不起作用,但给出了我正在尝试的逻辑的想法],获取其索引号并执行以下操作:
$('section:not(#clients)')。每个(函数(索引){
//如果有匹配的元素(每个部分),
if($(this).find(matchesEl).length>0){
//创建一个div.royalSlider并在该部分前面加上前缀
$('',{'class':'royalSlider'}).prependTo(this)
//对于本节中的每个匹配元素(我们不需要在此处获取索引号),
$(this).find(matchesEl).each(function(){
//将.rsImg类添加到此元素并插入div.royalmslider,
//不是任何div.royaSlider,而是之前给出的具有等效索引的div.royaSlider
$(this.addClass('rsImg').appendTo($('section.royalsloider').eq(index));
});
}
});
//此处不需要使用each()(两个索引都不需要)
//如果#clients中有匹配的元素,请执行以下操作:
if($('#clients').find(matchesEl).length>0{
//将附加到#客户端
$(“”)。附加到($(“#客户端”);
//将每个匹配元素放入#客户端
$('#clients').find(matchesEl).each(function(){
//创建一个- ,将元素内容附加到
- 中,并将其插入
到#clients中
$(“- ”).append($(this)).appendTo($('#clients').children('ul');
});
}
$('p').filter(函数(){return$.trim(this.innerHTML)==“”}.remove();
});
你好,利奥,我在前面添加了一条评论,感谢您的帮助-不知道发生了什么事。我还有最后一个问题,你可以帮我提出来-张贴-非常感谢你到目前为止所花的时间。