jQuery - 将类从一个元素复制到另一个 IF 条件是正确的

发布于 2024-10-31 20:16:36 字数 524 浏览 1 评论 0原文

我之后的基本想法..

在文档中,将类从“#box-one”复制到“#box-三”if“#box-two img”与“#”具有相同的图像名称box-one img”

,如果可能的话......可以从传输

html 中排除特定类:

<span id="box-one" class="first second third">
   <img src="imagefolder/image1.jpg" alt />
</span>

<span id="box-two">
   <img src="imagefolder2/image1.jpg" alt />
</span>

<span id="box-three"></span>

请记住,“#box-one”和“#box-two”内的图像会发生变化,但始终相同。

就js而言,我不知道如何做到这一点。这怎么能做到呢?

Basic idea of what im after..

On document ready copy classes from "#box-one" into "#box-three" if "#box-two img" has the same image name as "#box-one img"

and if possible.. with the possibility of excluding specific classes from transferring

html:

<span id="box-one" class="first second third">
   <img src="imagefolder/image1.jpg" alt />
</span>

<span id="box-two">
   <img src="imagefolder2/image1.jpg" alt />
</span>

<span id="box-three"></span>

Keeping in mind that the images inside "#box-one" and "#box-two" change but are always identical.

I have no idea how to do this as far as the js goes.. How could this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水染的天色ゝ 2024-11-07 20:16:36
$(document).ready(function(){

    var classesToIgnore = ["second"];

    if($("#box-one > img").attr("src").split('/').pop() == $("#box-two > img").attr("src").split('/').pop()) {
       //Get all classes into an array
       var classes = $("#box-one").attr("class").split(" ");
       //Grep out the classes you dont want and join it into a string seperated by space
       classes = $.grep(classes, function(class, index){
           return ($.inArray(class, classesToIgnore));
       }).join(" ");
       //Overwrite #box-three's classes. If you want to append them, loop over the array and use $("#box-three").addClass("className");
       $("#box-three").attr("class", classes);
    }

});

工作示例: http://jsfiddle.net/raPBy/

$(document).ready(function(){

    var classesToIgnore = ["second"];

    if($("#box-one > img").attr("src").split('/').pop() == $("#box-two > img").attr("src").split('/').pop()) {
       //Get all classes into an array
       var classes = $("#box-one").attr("class").split(" ");
       //Grep out the classes you dont want and join it into a string seperated by space
       classes = $.grep(classes, function(class, index){
           return ($.inArray(class, classesToIgnore));
       }).join(" ");
       //Overwrite #box-three's classes. If you want to append them, loop over the array and use $("#box-three").addClass("className");
       $("#box-three").attr("class", classes);
    }

});

Working example: http://jsfiddle.net/raPBy/

破晓 2024-11-07 20:16:36

这将添加从框一到框三的所有类,并清除框一的类。如果要移动特定类,可以使用 jQuery 方法 $.addClass$.removeClass

$(document).ready(function() {
  var box1 = $('#box-one'),
      box2 = $('#box-two'),
      box3 = $('#box-three'),
      className = box1.attr('class');

  if(box1.find('img').attr('src') === box2.find('img').attr('src')) {
      box3.addClass(className);
      box1.attr('class', '');
  }
});

编辑

您可以使用以下方法仅比较文件名段:

var src1 = $('#box-one').attr('src').split('/').pop(),
    src2 = $('#box-two').attr('src').split('/').pop();

This will add all of the classes from box one to box three, and clear the class of box one. If you want to move specific classes, you can use the jQuery methods $.addClass and $.removeClass.

$(document).ready(function() {
  var box1 = $('#box-one'),
      box2 = $('#box-two'),
      box3 = $('#box-three'),
      className = box1.attr('class');

  if(box1.find('img').attr('src') === box2.find('img').attr('src')) {
      box3.addClass(className);
      box1.attr('class', '');
  }
});

Edit

You can compare just the filename segment using:

var src1 = $('#box-one').attr('src').split('/').pop(),
    src2 = $('#box-two').attr('src').split('/').pop();
痴意少年 2024-11-07 20:16:36

试试这个:

$(document).ready(function(){
  if($('#box-two img').attr('alt') == $('#box-one img').attr('alt')){//can use attr('src')
   $('#box-three').addClass($('#box-one').attr('class'));
    $('#box-one').removeClass();//if u want to remove class from #box-one
  }
});

Try this:

$(document).ready(function(){
  if($('#box-two img').attr('alt') == $('#box-one img').attr('alt')){//can use attr('src')
   $('#box-three').addClass($('#box-one').attr('class'));
    $('#box-one').removeClass();//if u want to remove class from #box-one
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文