最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

href - Javascript: setAttribute works, getAttribute fails - Stack Overflow

programmeradmin1浏览0评论

I have this code:

var object1 = getElementByID('obj1');
alert(object1.getAttribute('href'));

This displays correctly the URL in the href attribute of the object. But, when I try:

var object1 = getElementByID('obj1');
object1.setAttribute('href','someotherURL');
alert(object1.getAttribute('href'));

This fails. The code doesn't work on FF so I can only test it in IE, no Firebug =/. I have also tried.

object1.href = "someotherURL";

but it also fails. Does anyone knows why I cant modify the attribute? let me know if I need to provide more information.

Regards.

UPDATE: HTML:

<table class="msrs-topBreadcrumb" cellpadding="0" cellspacing="0" border="0" width="100%">
    <tr>
        <td>
            <span>
                <div>
                    <a href="SomeURL">A Name</a> &gt;
                    <a href="AnotherURL">A Name2</a> &gt;
                    <a href="Third URL">A Name3</a>
                </div>
            </span>
        </td>
        <td align="right">
            <span>
                <a href="URL1">A Name</a>&nbsp;|
                <a href="URL2">A Name2</a>&nbsp;|
                <a href="URL3">A Name3</a>&nbsp;|
                <a href="URL4">A Name4</a>
            </span>
        </td>
    </tr>
</table>

FUNCTION:

function mySubscriptions()
{
    var mySubsObj = getElementsByClass('msrs-topBreadcrumb')[0].firstChild.childNodes[0].childNodes[1].childNodes[0].childNodes[2];
    alert(mySubsObj.getAttribute("href"));
}

I have this code:

var object1 = getElementByID('obj1');
alert(object1.getAttribute('href'));

This displays correctly the URL in the href attribute of the object. But, when I try:

var object1 = getElementByID('obj1');
object1.setAttribute('href','someotherURL');
alert(object1.getAttribute('href'));

This fails. The code doesn't work on FF so I can only test it in IE, no Firebug =/. I have also tried.

object1.href = "someotherURL";

but it also fails. Does anyone knows why I cant modify the attribute? let me know if I need to provide more information.

Regards.

UPDATE: HTML:

<table class="msrs-topBreadcrumb" cellpadding="0" cellspacing="0" border="0" width="100%">
    <tr>
        <td>
            <span>
                <div>
                    <a href="SomeURL">A Name</a> &gt;
                    <a href="AnotherURL">A Name2</a> &gt;
                    <a href="Third URL">A Name3</a>
                </div>
            </span>
        </td>
        <td align="right">
            <span>
                <a href="URL1">A Name</a>&nbsp;|
                <a href="URL2">A Name2</a>&nbsp;|
                <a href="URL3">A Name3</a>&nbsp;|
                <a href="URL4">A Name4</a>
            </span>
        </td>
    </tr>
</table>

FUNCTION:

function mySubscriptions()
{
    var mySubsObj = getElementsByClass('msrs-topBreadcrumb')[0].firstChild.childNodes[0].childNodes[1].childNodes[0].childNodes[2];
    alert(mySubsObj.getAttribute("href"));
}
Share Improve this question edited Nov 13, 2013 at 17:46 varrtto asked Nov 13, 2013 at 17:34 varrttovarrtto 1012 gold badges3 silver badges11 bronze badges 5
  • It'd help to know what HTML element is involved! – Pointy Commented Nov 13, 2013 at 17:37
  • 3 It's getElementById not getElementByID. – j08691 Commented Nov 13, 2013 at 17:37
  • Also you don't need setAttribute() and getAttribute() to access or modify properties like "href". – Pointy Commented Nov 13, 2013 at 17:40
  • I'm sorry, this is just a sample of what the code actually does. I'll provide a more plete code. – varrtto Commented Nov 13, 2013 at 17:41
  • 1 so I can only test it in IE, no Firebug =/. IE has a debugger! Are you using IE7 which is the last version of a browser without the console? lol – epascarello Commented Nov 13, 2013 at 17:54
Add a ment  | 

1 Answer 1

Reset to default 4

Use document.getElementById and it works fine:

var object1 = document.getElementById('obj1');
object1.setAttribute('href','someotherURL');
alert(object1.getAttribute('href'));

Tested in Firefox, IE and Chrome.

Demo: http://jsfiddle/Guffa/zdrP9/

发布评论

评论列表(0)

  1. 暂无评论