javascript-查找元素并将其移动到另一个元素下方
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-查找元素并将其移动到另一个元素下方,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2915字,纯文字阅读大概需要5分钟。
内容图文
我想将包含文本“我的愿望清单”的href的元素.nav.item移动到div#move-below-this下面,大致像这样:
$(".nav.item").insertAfter("#move-below-this");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="list">
<div id="move-below-this"></div>
</div>
<ul class="nav items">
<li class="nav item current"><strong>Account Dashboard</strong></li>
<li class="nav item"><a href="http://testa.dev/sales/order/history/">My Orders</a></li>
<li class="nav item"><a href="http://testa.dev/downloadable/customer/products/">My Downloadable Products</a></li>
<li class="nav item"><a href="http://testa.dev/wishlist/">My Wish List</a></li> <!--I want to move this element -->
<li class="nav item">
<span class="delimiter"></span>
</li>
<li class="nav item"><a href="http://testa.dev/customer/address/">Address Book</a></li>
<li class="nav item"><a href="http://testa.dev/customer/account/edit/">Account Information</a></li>
<li class="nav item"><a href="http://testa.dev/vault/cards/listaction/">Stored Payment Methods</a></li>
<li class="nav item"><a href="http://testa.dev/paypal/billing_agreement/">Billing Agreements</a></li>
<li class="nav item">
<span class="delimiter"></span>
</li>
<li class="nav item"><a href="http://testa.dev/review/customer/">My Product Reviews</a></li>
<li class="nav item"><a href="http://testa.dev/newsletter/manage/">Newsletter Subscriptions</a></li>
</ul>
解决方法:
您可以使用:contains()选择器通过文本内容查找元素.
但是,在这种情况下,您需要确保移动后的li仍然包含在ul中,以保持HTML有效.为此,您可以使用wrap().parent(),如下所示:
$('.nav.item:contains("My Wish List")').wrap('<ul />').parent().insertAfter('#move-below-this');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="list">
<div id="move-below-this"></div>
</div>
<ul class="nav items">
<li class="nav item current"><strong>Account Dashboard</strong></li>
<li class="nav item"><a href="http://testa.dev/sales/order/history/">My Orders</a></li>
<li class="nav item"><a href="http://testa.dev/downloadable/customer/products/">My Downloadable Products</a></li>
<li class="nav item"><a href="http://testa.dev/wishlist/">My Wish List</a></li> <!--I want to move this element -->
<li class="nav item">
<span class="delimiter"></span>
</li>
<li class="nav item"><a href="http://testa.dev/customer/address/">Address Book</a></li>
<li class="nav item"><a href="http://testa.dev/customer/account/edit/">Account Information</a></li>
<li class="nav item"><a href="http://testa.dev/vault/cards/listaction/">Stored Payment Methods</a></li>
<li class="nav item"><a href="http://testa.dev/paypal/billing_agreement/">Billing Agreements</a></li>
<li class="nav item">
<span class="delimiter"></span>
</li>
<li class="nav item"><a href="http://testa.dev/review/customer/">My Product Reviews</a></li>
<li class="nav item"><a href="http://testa.dev/newsletter/manage/">Newsletter Subscriptions</a></li>
</ul>
内容总结
以上是互联网集市为您收集整理的javascript-查找元素并将其移动到另一个元素下方全部内容,希望文章能够帮你解决javascript-查找元素并将其移动到另一个元素下方所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。