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>