HTML 代码:<p>Hello <a href="http://ejohn.org/">John</a>, how are you doing?</p> 
jQuery 代码:$("p").contents().not("[nodeType=1]").wrap("<b/>"); 
结果:
<p><b>Hello</b> <a href="http://ejohn.org/">John</a>, <b>how are you doing?</b></p> 这是手册上的例子,但我在火狐运行以后是下面的结果:
<p>Hello<b><a href="http://ejohn.org/">John</a></b>, how are you doing?</p>
而且在IE下是都没有加粗。这是为什么呢?

解决方案 »

  1.   

    $("p").contents().each(function(){if(this.nodeType == 1){$(this).wrap('<b/>')}})
      

  2.   

    那写成这样:
    $("p").contents().not("[nodeType==1]").wrap('<b/>');为什么a元素还加粗啊?not("[nodeType==1]")是应该排除元素的节点啊?
      

  3.   

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script src="commonjs/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(window).ready(function(){
    $('button').click(function(){
    $("p").contents().not("[nodeType=1]").wrap("<b/>"); 
    });
    });
    </script>
    </head>
    <body>
    <p><b>Hello</b> <a href="http://ejohn.org/">John</a>, <b>how are you doing?</b></p> 
    <button></button>
    </body>
    </html>
      

  4.   

    <body>
    <p>Hello <a href="http://ejohn.org/">John</a>, how are you doing?</p> 
    <button></button>
    </body>
    上面写错了,
    [nodetype=1]应该指的是文本节点,不然的话直接写个a就行了,去掉文本节点后,a元素套上<b>就加粗了,
    手册上写错了吧
      

  5.   

    $("p").contents().not("[@nodeType=1]").wrap("<b/>"); 手册上是有@的,你怎么没有写啊!!!