开发者

how can i read an element except specific child element?

sorry for bad english. i want to read elemen开发者_StackOverflow社区t with "a" id but no "b" element in result?

<div id="a">
hi
<div id="b">
every one
</div>
<div>bye</div>
</div>

and result should be

 hi <div>bye</div>

how to do this?


It looks like you want an HTML string as the result.

You could make a .clone() of #a, remove the #b from the clone, then get the .html() content.

Example: http://jsfiddle.net/uZ97k/

var html = $('#a').clone().children().remove('#b').end().html();

or it could be written like this:

Example: http://jsfiddle.net/uZ97k/1/

var html = $('#a').clone().children('#b').remove().end().html();


Perhaps the jQuery not() selector method will work well for you.

Edit: Please note that I interpreted your question literally. This will select elements that do not match a certain property. If you were making a generalization with your example code, then this might not work for what you wish. It might be helpful if you could clarify further.


Wrap "hi" in a div tag like this

<div id="a">
  **<div> hi </div>**
<div id="b">
every one
</div>
<div>bye</div>
</div>

Then

<script type="text/javascript">
    var result = $("#a").children(":not(#b)").html();
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜