Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery在DOM元素内进行DOM更改_Jquery_Dom_This - Fatal编程技术网

jQuery在DOM元素内进行DOM更改

jQuery在DOM元素内进行DOM更改,jquery,dom,this,Jquery,Dom,This,我有这样的4个div: <div class="content"> <div class="col"> <h3>A title</h3> <img src="..." alt=""> </div> <div class="col"> <h3>Another title</h3> <img src=

我有这样的4个div:

<div class="content">
    <div class="col">
        <h3>A title</h3>
        <img src="..." alt="">
    </div>
    <div class="col">
        <h3>Another title</h3>
        <img src="..." alt="">
    </div>
    <div class="col">
        <h3>A title</h3>
        <img src="..." alt="">
    </div>
    <div class="col">
        <h3>Another title</h3>
        <img src="..." alt="">
    </div>
</div>
我尝试了一些调整,比如
$('.col:odd img').insertAfter(“h3”)

<div class="content">
    <div class="col">
        <img src="..." alt="">
        <h3>A title</h3>
    </div>
    <div class="col">
        <h3>Another title</h3>
        <img src="..." alt="">
    </div>
    <div class="col">
        <img src="..." alt="">
        <h3>Another title</h3>
    </div>
    <div class="col">
        <h3>Another title</h3>
        <img src="..." alt="">
    </div>
</div>
还有很多蹩脚的代码,但是没有找到一个好的方法。 我的Google/Stackoverflow搜索似乎没有返回好结果。我可能没有好好搜查过

有做这件事的线索/想法吗

谢谢你,对不起,英文版:/

你可以用

  • 选择替换图元的选择器
  • 用于在元素上循环
  • 选择当前图元内的图元的步骤
  • 在传递的元素之后移动元素
  • 代码:

    $('.content.col:偶数')。每个(函数(){
    $('h3',this).insertAfter($('img',this));
    });
    
    
    头衔
    另一个标题
    头衔
    另一个标题
    
    试试这个

    $(文档).ready(函数(){
    $.each($(“.col”),函数(键,val){
    $($(val.children()[0])。之前($($(val.children()[1]);
    });		
    });
    
    
    头衔
    另一个标题
    头衔
    另一个标题
    
    first
    index
    将从
    0
    开始,这不是
    odd
    ,但op正在考虑从
    1
    开始,这是
    odd
    。我知道我遗漏了什么。我拥有所有元素,但我没有考虑
    。each()
    。感谢Tushar和“上下文选择器”文档:)
    var myImg = $( "h3" );
    $('.col:odd').find( myImg ).insertAfter( "h3" );
    
    $('.content .col:even').each(function () {
        $('h3', this).insertAfter($('img', this));
    });