Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

javascript - 移除href值(如果使用jquery存在)(Removing an href value, if it exists using jquery)

What would the best way to remove an href that has a specific value using jquery if it is found in the DOM.(如果在DOM中找到了使用jquery删除具有特定值的href的最佳方法是什么?)

 $(document).ready(function () {
        $('a').each(function () {
            var hrefValue = $(this).attr("href")
            if (hrefValue == '/remove/thehrefvalue?when=now') {
               //remove only a href containing this specific value
            }

        });
    });

Kind regards(亲切的问候)

  ask by Arianule translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of iterating through every a , you can iterate through only a s which have that particular href attribute by altering the selector string:(无需遍历每个 a ,您可以通过更改选择器字符串来仅遍历具有特定href属性a s:)

$('a[href="/remove/thehrefvalue?when=now"]').remove();

 $('a[href="/remove/thehrefvalue?when=now"]').remove(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="/remove/thehrefvalue?when=now">now</a> <a href="/remove/thehrefvalue?when=now">now</a> <a href="/remove/thehrefvalue?when=notnow">notnow</a> <a href="/remove/thehrefvalue?when=now">now</a> 

(Not entirely sure what you're looking for. If you wanted to remove the <a> s, use the code above - if you wanted to remove the attributes but leave the <a> s alone, use removeAttr('href') instead of .remove() )((不完全确定您要查找的内容。如果要删除<a> ,请使用上面的代码-如果要删除属性,但不使用<a> ,请使用removeAttr('href')而不是.remove() ))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...