改变“id=”的值使用 document.getElementById

发布于 2024-08-04 03:02:07 字数 492 浏览 5 评论 0原文

这是正确的方法吗?

<a id="load" href="#" class="btn load" onclick="request(this); $('#load').fadeOut(2000)">add</a>

<a href="#" id="test" onClick="alert( 'TEST!' )">here</a>

<script language="javascript">
var requests = 2;

function request(self)
{if(self.href != "#")
requests -= 1;

if(requests === 0)
document.getElementById("test").id= "nextstep";}
</script>

如果请求 = 0,我希望 id="test" 更改为 id="nextstep"。

Is this the correct way to do it?

<a id="load" href="#" class="btn load" onclick="request(this); $('#load').fadeOut(2000)">add</a>

<a href="#" id="test" onClick="alert( 'TEST!' )">here</a>

<script language="javascript">
var requests = 2;

function request(self)
{if(self.href != "#")
requests -= 1;

if(requests === 0)
document.getElementById("test").id= "nextstep";}
</script>

I want id="test" to change to id="nextstep" if the requests = 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如梦 2024-08-11 03:02:07

你可以尝试:

document.getElementById('test').setAttribute('id', 'nextstep');

You could try:

document.getElementById('test').setAttribute('id', 'nextstep');
〃安静 2024-08-11 03:02:07

这正是您要做的事情,只要您将其包装在脚本标记中...

<script language="javascript" type="text/javascript">
if(requests === 0){
    document.getElementById('test').id = "nextstep";
}
</script>

如果您尝试通过单击该锚点来触发此操作,那么您可以使用:

<script language="javascript" type="text/javascript">
function changeID(){
    if(requests === 0){
        document.getElementById('test').id = "nextstep";
        alert('TEST!');
    }
}
</script>

<a href="#" id="test" onclick="changeID()">Test</a>

编辑:< /strong> 那么,在我发布此内容后,您更改了问题的内容......

That's exactly how you'd go about doing it, as long as you wrap it in script tags...

<script language="javascript" type="text/javascript">
if(requests === 0){
    document.getElementById('test').id = "nextstep";
}
</script>

If you're trying to trigger this on the click of that anchor, then you'd use:

<script language="javascript" type="text/javascript">
function changeID(){
    if(requests === 0){
        document.getElementById('test').id = "nextstep";
        alert('TEST!');
    }
}
</script>

<a href="#" id="test" onclick="changeID()">Test</a>

EDIT: Well you changed the content of your question after I posted this...

卷耳 2024-08-11 03:02:07

您需要使用“setAttribute”来更改锚标记的ID。

你已经得到了 getElementById('test') 部分,但只需抛出“.setAttribute('id','nextstep');”

希望有帮助!

You need to use 'setAttribute' to change the ID of anchor tag.

You've got the getElementById('test') part right, but just throw ".setAttribute('id','nextstep');"

Hope that helps!

梦行七里 2024-08-11 03:02:07

if(self.href != "#")

这就是你的问题。链接 href 由浏览器规范化,因此页面 'http://www.example.com/ 上的链接 '#' ' 的 href 属性为 'http://www.example.com/# ' 并且与条件不匹配。

避免使用名称“self”,因为它与全局对象(也称为“窗口”)的名称冲突。您可以通过从 JavaScript 分配处理程序来避免笨拙地传入“self”:

document.getElementById('load').onclick= request;

而不是 onclick 属性,然后您可以在 request() 中使用“this”。 request() 还应该返回 false; 以停止跟踪链接(使浏览器滚动到顶部)。更好的是使用

不要对 HTML 使用 setAttribute(),它在 IE 中存在与此用法无关的问题。设置 element.id 已经很好了。

但无论如何,这一切似乎都是一种很奇怪的方式来实现你想做的事情。

if(self.href != "#")

There's your problem. Link hrefs are canonicalised by the browser, so a link ‘#’ on page ‘http://www.example.com/’ has an href attribute of ‘http://www.example.com/#’ and does not match the condition.

Avoid using the name ‘self’, as it clashes with the name of the global object (aka ‘window’). You can avoid the clumsy passing in of ‘self’ by assigning the handler from JavaScript:

document.getElementById('load').onclick= request;

instead of the onclick attribute, then you could use ‘this’ in request(). request() should also return false; to stop the link being followed (making the browser scroll to the top). Better still would be to use a <button> (or <input type="button">), because it's an action button and not a link that you could open in a new window or bookmark. (You can always use CSS to make it look less like a button.)

Don't use setAttribute() for HTML, it has problems in IE unrelated to this usage. Setting element.id was already fine.

But this all seems like rather a strange way of going about what you want to do in any case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文