简体   繁体   English

在图像后找到闭合锚标签

[英]Find closing anchor tag after image

I would like to move a div to the spot right after a closing anchor tag. 我想在关闭锚标记后立即将div移动到该位置。 All I have been given is the image classname. 我得到的只是图像类名。 How do I find the closing anchor tag wrapping the image? 如何找到包裹图像的闭合锚标签?

<a href="#"><img class="cat-image" src="http://placekitten.com/200/300" title="Funky roots" /></a>

On JS Fiddle I have an example where I iterate through a list looking for an image with a certain class name. 在JS Fiddle上,我有一个示例,其中遍历列表以查找具有特定类名的图像。 If that image exists I would like to move the '.moveMe' to after the closing anchor tag. 如果该图像存在,我想将'.moveMe'移动到结束锚标记之后。

Unfortunately I can't modify the html. 不幸的是,我无法修改html。 I can't add an id or class to the anchor or image tags or wrap the whole thing in a div. 我无法将ID或类添加到锚点或图像标签,也无法将整个内容包装在div中。

HTML: HTML:

<ul class='testList'>
    <li class="listItem-0">
        <div class="moveMe"></div> <pre>
        <div class="wrapper">
            <a href="#"><img class="cat-image" src="http://placekitten.com/200/300" title="Funky roots" /></a><!-- moveMe div here -->
        </div>
        <div class="summary"></div>
        </pre>

    </li>
    <li class="listItem-1">
        <div class="moveMe"></div>  <pre>
        <div class="wrapper">
            <div></div>
        </div>
        <div class="summary"></div>
        </pre>

    </li>
    <li class="listItem-2">
        <div class="moveMe"></div>  <pre>
        <div class="wrapper">
            <a href="#"><img class="cat-image" src="http://placekitten.com/300/400" title="bigger cat pic" /></a>
        </div>
        <div class="summary"></div>
        </pre>

    </li>
</ul>

JS: JS:

var listItems = $(".testList li");

listItems.each(function (idx, li) {
    if ($(li).find('.cat-image').length) {
        console.log('listItemContent image==>', $('pre').find('.cat-image'));  
        /*$(li).find('???? </a>  ????').append($(li).find('.moveMe'));*/
    }
    console.log($('.testList').html());
});

JS Fiddle JS小提琴

Try to use .after() in this context, 尝试在这种情况下使用.after()

var listItems = $(".testList li");
var cache = null;

listItems.each(function (idx, li) {
    cache = $(li).find('.cat-image');
    if (cache.length) {
      cache.parent().after($(li).find('.moveMe'));
    }
});

DEMO DEMO

You can do this as follows: 您可以按照以下步骤进行操作:

var listItems = $(".testList li");
listItems.each(function (idx, li) {
    var jli = $(li);
    jli.find(".cat-image")
       .closest("a")
       .after(jli.find(".moveMe"));
});

which, if you like, can be reduced to a single line: 如果愿意,可以将其简化为一行:

var listItems = $(".testList li");
listItems.each(function (idx, li) {
    $(li).find(".cat-image").closest("a").after($(li).find(".moveMe"));
});

http://jsfiddle.net/MBTNU/5/ http://jsfiddle.net/MBTNU/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM