jQuery在DOM元素内进行DOM更改
我有这样的4个div: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=
<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]);
});
});代码>
头衔
另一个标题
头衔
另一个标题
firstindex
将从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));
});