将 class 名称从 child 移动到 parent

Move class name from child to parent

希望使用 jQuery 将图像的 class 名称移动到它包含图形元素...

<figure class="wp-caption">
    <img class="imghvr-hinge-up" src="image.jpg">
    <figcaption >A sample caption</figcaption>
</figure>

特别是我想将任何以 'imghvr-' 开头的 img 标签 class 名称移动到它包含的图形中,如果包含图形的名称为 class 'wp-caption' .结果如下所示...

<figure class="wp-caption imghvr-hinge-up">
    <img class="" src="image.jpg">
    <figcaption >A sample caption</figcaption>
</figure>

希望这是有道理的!)任何帮助将不胜感激!

你可以这样做

$('img[class^="imghvr-"]').each(function() {
  var cl = $(this).attr('class');
  $(this).removeClass().parent('[class="wp-caption"]').addClass(cl);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<figure class="wp-caption">
  <img class="imghvr-hinge-up" src="image.jpg">
  <figcaption>A sample caption</figcaption>
</figure>

var imgs = document.querySelectorAll(".wp-caption img");

    for(var i=0; i<imgs.length; i++) {
        var img = imgs[i];
        if(img.className.startWith("imghvr-")) {
            img.parentChild.classList.push(img.className);
            img.className = '';
        }

    }

类似的东西。我没有测试。

编辑:一如既往的比我聪明^^

$( 'img[class^="imghvr-"]' ).each( function() {
  var imghvrClass =  $(this).attr('class').match(/imghvr-.*/)[0]
  var $parent = $(this).parent()

  if ( $parent.hasClass( 'wp-caption' ) ) {
    $(this).removeClass( imghvrClass )
    $parent.addClass( imghvrClass )  
  }
})


<figure class="wp-caption">
    <img class="imghvr-hinge-up" src="image.jpg">
    <figcaption >A sample caption</figcaption>
</figure>
<figure class="wp-caption">
    <img class="imghvr-hinge-up2" src="image.jpg">
    <figcaption >A sample caption</figcaption>
</figure>

您可以利用 .closest().addClass(function(){}).is()

var c = "[class^=imghvr-]";

$("img" + c)
  .closest("figure")
  .addClass(function() {
    if ($(this).is(".wp-caption")) {
      var img = $(c, this);
      var imgClass = img[0].className;
      img[0].className = "";
      return imgClass;
    }
  })
img[class^="imghvr-"] {
  border: 1px solid blue;
}
figure[class*="imghvr-"] {
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<figure class="wp-caption">
  <img class="imghvr-hinge-up" src="image.jpg">
  <figcaption>A sample caption</figcaption>
</figure>

<figure class="wp-caption">
  <img class="imghvr-hinge-up" src="image.jpg">
  <figcaption>A sample caption</figcaption>
</figure>

<figure class="wp">
  <img class="imghvr-hinge-up" src="image.jpg">
  <figcaption>A sample caption</figcaption>
</figure>

或者,使用 .toggleClass().is().find().attr()

var c = "[class^=imghvr-]";

$("figure.wp-caption").toggleClass(function() {
  var img = $("img", this);
  return img.is(c) ? img.attr("class") : false
}).find(c).attr("class", "");
img[class^="imghvr-"] {
  border: 1px solid blue;
}
figure[class*="imghvr-"] {
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<figure class="wp-caption">
  <img class="imghvr-hinge-up" src="image.jpg">
  <figcaption>A sample caption</figcaption>
</figure>

<figure class="wp-caption">
  <img class="imghvr-hinge-up" src="image.jpg">
  <figcaption>A sample caption</figcaption>
</figure>

<figure class="wp">
  <img class="imghvr-hinge-up" src="image.jpg">
  <figcaption>A sample caption</figcaption>
</figure>