jQuery replaceWith不起作用 [英] Jquery replaceWith not working

查看:544
本文介绍了jQuery replaceWith不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Jquery 1.7.1

Using Jquery 1.7.1

我有两个div

<div class="highlightStart"></div>
{page content here}
<div class="highlightEnd"></div> 

那些出现在页面源中.但是我在页面底部添加的这个jQuery无法正常工作:

Those show up in the page source. But this jquery I added at the bottom of the page is not working:

<script type="text/javascript" id="makeHighlight">
   $(document).ready(function () {
     $("div.highlightStart").replaceWith("<section class='highlight'>");
     $("div.highlightEnd").replaceWith("</section>"); 
   });
</script>

浏览器控制台(Chrome)中没有显示JavaScript错误.只是什么都没有取代.

No javascript errors showing in the browser console (Chrome). Just nothing gets replaced.

推荐答案

基于isherwood的帮助,将其用作解决方案:

Based on help from isherwood, used this as the solution:

http://jsfiddle.net/H36UE/1/

使用这样的HTML树:

With HTML tree like this:

<div><div><div class="highlightStart">highlightStart</div></div></div>
<div>Outside<div>Content to Highlight</div>More</div>
<div>second</div>
<div>third</div>
<div><div><div class="highlightEnd">highlightEnd</div></div></div>

此Javascript:

This Javascript:

$(document).ready(function () {
  $('.highlightStart').parent().parent().replaceWith("<div class='highlightStart'>");
  $('.highlightEnd').parent().parent().replaceWith("<div class='highlightEnd'>");
  $('.highlightStart').nextUntil('.highlightEnd').andSelf().wrapAll("<section class='highlight'>");

 $('.highlightStart, .highlightEnd').remove();
});

这篇关于jQuery replaceWith不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆