jquery根据查询字符串删除标签

时间:2018-06-14 14:20:14

标签: javascript jquery html

我在div中的p标签内有一个图像列表

如果UserProfile等于abasket=empty中的一个,我如何使用jquery删除semi代码?

abandoned

4 个答案:

答案 0 :(得分:5)

URL.searchParams在这里很有用

$("#userShop a").each(function(link) {
  if (["empty","semi","abandoned"].indexOf(
       new URL(this.href).searchParams.get("basket")
     ) !=-1) {
    $(this).remove();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="userShop">
  <p class="shoppingbaskets">
    <a href="RunFunction.jsp;jsessionid=123456789?shop=querty&basket=empty"><img src="image/empty.png" title="empty" width="50" height="50"></a>
    <a href="RunFunction.jsp;jsessionid=123456789?shop=querty&basket=full"><img src="image/full.png" title="full" width="50" height="50"></a>
    <a href="RunFunction.jsp;jsessionid=123456789?shop=querty&basket=semi"><img src="image/semi.png" title="semi" width="50" height="50"></a>
    <a href="RunFunction.jsp;jsessionid=123456789?shop=querty&basket=abandoned"><img src="image/abandoned.png" title="abandoned" width="50" height="50"></a>
    <a href="RunFunction.jsp;jsessionid=123456789?shop=querty&basket=completed"><img src="image/completed.png" title="completed" width="50" height="50"></a>
  </p>
</div>

答案 1 :(得分:2)

href是一个属性,因此您可以通过属性选择器使用它,并使用$匹配,如果它以您的参数结尾

var params = ["empty","semi","abandoned"];
for(var i in params){
 $("a[href$='basket=" + params[i] + "']").remove();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="userShop">
  <p class="shoppingbaskets">
    <a href="RunFunction.jsp;jsessionid=123456789?shop=querty&basket=empty"><img src="image/empty.png" title="empty" width="50" height="50"></a>
    <a href="RunFunction.jsp;jsessionid=123456789?shop=querty&basket=full"><img src="image/full.png" title="full" width="50" height="50"></a>
    <a href="RunFunction.jsp;jsessionid=123456789?shop=querty&basket=semi"><img src="image/semi.png" title="semi" width="50" height="50"></a>
    <a href="RunFunction.jsp;jsessionid=123456789?shop=querty&basket=abandoned"><img src="image/abandoned.png" title="abandoned" width="50" height="50"></a>
    <a href="RunFunction.jsp;jsessionid=123456789?shop=querty&basket=completed"><img src="image/completed.png" title="completed" width="50" height="50"></a>
  </p>
</div>

答案 2 :(得分:1)

隐藏包含在属性

中的值的元素
$("a[href*='basket=empty']").hide();
$("a[href*='basket=semi']").hide();
$("a[href*='basket=abandoned']").hide();

删除包含在属性

中的值的元素
$("a[href*='basket=empty']").remove();
$("a[href*='basket=semi']").remove();
$("a[href*='basket=abandoned']").remove();

答案 3 :(得分:-2)

你可以使用.html()属性并重写父div“userShop”的所有内容,有些像这样:

if(yourcondition){
$("#userShop").html(".............all the new content.......");
}