首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

jquery 怎么刪除節點

2012-06-02 
jquery 如何刪除節點JScript code$.get(url,function(data){if(data abc){$(#Result).each(functio

jquery 如何刪除節點

JScript code
$.get(        url,        function(data){            if(data == "abc"){                $("#Result")                    .each(function(i){                        // 服務器返回了“abc”,意味著我要刪掉下面 html 裏                        // “<div class="Item"><span class="ItemValue">abc</span></div>”                        // 這行。                        // 請問這個代碼這裡該怎麽寫?謝謝                        }                    });            }        }    );


HTML code
<div id="Result">    <div class="Item"><span class="ItemValue">123</span></div>    <div class="Item"><span class="ItemValue">abc</span></div>    <div class="Item"><span class="ItemValue">xyz</span></div></div>


[解决办法]
:contains()选择器会选择所有文本包含abc的元素(比如1abc、abcd),也可能不适用于你的需求,精确匹配的话还得通过遍历比较:

HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script></head><body><div id="Result">    <div class="Item"><span class="ItemValue">123</span></div>    <div class="Item"><span class="ItemValue">abc</span></div>    <div class="Item"><span class="ItemValue">xyz</span></div></div><script type="text/javascript">var data = 'abc';if (data == 'abc') {    $("#Result .ItemValue").each( function() {        if ($(this).text() == 'abc') $(this).parent().remove();    });}</script></body></html> 

热点排行