改变“id=”的值使用 document.getElementById
这是正确的方法吗?
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以尝试:
You could try:
这正是您要做的事情,只要您将其包装在脚本标记中...
如果您尝试通过单击该锚点来触发此操作,那么您可以使用:
编辑:< /strong> 那么,在我发布此内容后,您更改了问题的内容......
That's exactly how you'd go about doing it, as long as you wrap it in script tags...
If you're trying to trigger this on the click of that anchor, then you'd use:
EDIT: Well you changed the content of your question after I posted this...
您需要使用“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!
这就是你的问题。链接 href 由浏览器规范化,因此页面 'http://www.example.com/ 上的链接 '#' ' 的 href 属性为 'http://www.example.com/# ' 并且与条件不匹配。
避免使用名称“self”,因为它与全局对象(也称为“窗口”)的名称冲突。您可以通过从 JavaScript 分配处理程序来避免笨拙地传入“self”:
而不是
onclick
属性,然后您可以在 request() 中使用“this”。 request() 还应该返回 false;
以停止跟踪链接(使浏览器滚动到顶部)。更好的是使用不要对 HTML 使用 setAttribute(),它在 IE 中存在与此用法无关的问题。设置
element.id
已经很好了。但无论如何,这一切似乎都是一种很奇怪的方式来实现你想做的事情。
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:
instead of the
onclick
attribute, then you could use ‘this’ in request(). request() should alsoreturn 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.