用于XPATH的JQuery(!=)无法正常工作

时间:2011-01-15 14:27:44

标签: jquery html

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("[href!='http://www.google.net']").hide();
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p class="waqar">This is a paragraph.</p>
<p class="wr">This is another paragraph.</p>
<a href="http://www.google.net">google</a>
<a href="http://www.yahoo.com">yahoo</a>
<button>Click me</button>
</body>
</html>

当我运行此代码时,整个页面变为空白。请帮忙

1 个答案:

答案 0 :(得分:3)

$("[href!='http://www.google.net']")选择所有项目(标记),其中“href”属性不等于“http://www.google.net”。例如,<body>标记没有属性“href”=&gt;它的价值不等于“http://www.google.net”=&gt;它应该被隐藏。

试试这个:

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("a[href!='http://www.google.net']").toggle();
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p class="waqar">This is a paragraph.</p>
<p class="wr">This is another paragraph.</p>
<a href="http://www.google.net">google</a>
<a href="http://www.yahoo.com">yahoo</a>
<button>Click me</button>
</body>
</html>