javascript - How to reference <a> within <li> with jQuery - Stack Overflow

I want to replace the HREFs in a list with new URLs.I have something like this:<ul id="sidebarI

I want to replace the HREFs in a list with new URLs.

I have something like this:

<ul id="sidebarItems">
   <li><a href="/PartBasicInfo/XX">Part Basic Info</a></li>
   <li><a href="/SupplierContracts/XX">Supplier Contracts</a></li>
</ul>

I'm trying this (I know there are lots of ways to do it), but not having any luck. Any ideas?

function SetUrlParams() {
        $("#sidebarItems > li > a").each(function (idx, a) {
            a.attr("href", "MyURLOfChoice");
        });

I want to replace the HREFs in a list with new URLs.

I have something like this:

<ul id="sidebarItems">
   <li><a href="/PartBasicInfo/XX">Part Basic Info</a></li>
   <li><a href="/SupplierContracts/XX">Supplier Contracts</a></li>
</ul>

I'm trying this (I know there are lots of ways to do it), but not having any luck. Any ideas?

function SetUrlParams() {
        $("#sidebarItems > li > a").each(function (idx, a) {
            a.attr("href", "MyURLOfChoice");
        });
Share Improve this question edited May 7, 2012 at 16:35 gdoron 150k59 gold badges302 silver badges371 bronze badges asked May 7, 2012 at 15:24 birdusbirdus 7,52417 gold badges64 silver badges95 bronze badges 5
  • Also, why am I not seeing the formatting toolbar when I make my posts? Would like to mark code blocks. – birdus Commented May 7, 2012 at 15:25
  • Re: your ment, no idea. It works just fine for me. Ask on Meta. – Matt Ball Commented May 7, 2012 at 15:27
  • Re the disappearing formatting toolbar, I don't think it has anything to do with you particularly. That happens to me once in a great while. Refreshing the page can fix it. – DOK Commented May 7, 2012 at 15:32
  • I've verified that the loop is executing and that "this.href" is, in fact, the old hyperlink. However, when the loop is done executing and I hover over the menu items, they still have their old values. – birdus Commented May 7, 2012 at 15:58
  • @birdus Use $(this).attr('href', "foo") if you want to change the HTML element, not just the variable. – gdoron Commented May 7, 2012 at 16:36
Add a ment  | 

5 Answers 5

Reset to default 5

The second parameter is the DOM element, not the jQuery element, wrap a with $(a)

function SetUrlParams() {
    $("#sidebarItems > li > a").each(function(idx, a) {
        $(a).attr("href", "MyURLOfChoice");
    });
}​

Or leave jQuery for this simple task:

function SetUrlParams() {
    $("#sidebarItems > li > a").each(function(idx, a) {
        a.href = "MyURLOfChoice";
    });
}​

Note that you can access the DOM element with this instead of a.

Try this where this refers to each of the anchor element in the matched set.

 function SetUrlParams() {
    $("#sidebarItems > li > a").each(function(){
       this.href = "MyURLOfChoice";
    });
 }

Note that in your code a refers to the dom element so you should convert it to jQuery object before calling any jQuery method if you want to keep your code.

$('#sidebaritems > li > a').each( function(){
    this.href = newURL;
});

The second parameter in the function passed to each will be the DOM element, not a jQuery object. You need to use $(a), or just $(this) would work as well.

i tested this and it is working here

$("#sidebarItems > li > a").each(function (idx, a) {
        $(a).attr("href", "MyURLOfChoice");
    });

it appears that a parameter is a HtmlElement, not wrapped by jquery

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745163807a4614525.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信