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
98 views
in Technique[技术] by (71.8m points)

html - How to continue jQuery Function

I am using the following code to change href links in a page to a new link by using their id.

This is what I'm using to find the href and add the id to it;

    $( document ).ready(function() {
  $('a[href$="/truck-bed-covers/camper-tops"]').attr('id', 'camper1');
});

And this is what I'm using to change the link.

    $(document).ready(function () {
$("#camper1").attr("href", "../camper-tops");
});

It works great. Except it doesn't continue on the rest of the page. It only changes one link and then it's done. How do I continue until there is no more links to change?

question from:https://stackoverflow.com/questions/65908302/how-to-continue-jquery-function

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

1 Answer

0 votes
by (71.8m points)

ID has to be unique, else JavaScript works with the first one only.

$(document).ready(function() {
    $('a[href$="/truck-bed-covers/camper-tops"]').each(function(){
        $(this).attr("href", "../camper-tops");
    });
});

But I don't think this is the right way. You should find place where you create incorrect links and repair it there (in PHP/DB or where links came from).


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

...