Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

javascript - 如何通过javascript更改按钮上的<a>标签的href(How to change href of <a> tag on button click through javascript)

How to change the href attribute value of an <a/> tag through Javascript on button click ?(如何在按钮单击时通过Javascript更改<a/>标记的href属性值?)

<script type="text/javascript"> function f1() { document.getElementById("abc").href="xyz.php"; } </script> <a href="" id="abc">jhg</a> <a href="" id="" onclick="f1()">jhhghj</a>   ask by Chauhan translate from so

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

1 Reply

0 votes
by (71.8m points)

Without having a href , the click will reload the current page, so you need something like this:(没有href ,点击将重新加载当前页面,所以你需要这样的东西:)

<a href="#" onclick="f1()">jhhghj</a> Or prevent the scroll like this:(或者像这样阻止滚动:) <a href="#" onclick="f1(); return false;">jhhghj</a> Or return false in your f1 function and:(或者在f1函数中return false并且:) <a href="#" onclick="return f1();">jhhghj</a> ....or, the unobtrusive way:(....或者,不引人注目的方式:) <a href="#" id="abc">jhg</a> <a href="#" id="myLink">jhhghj</a> <script type="text/javascript"> document.getElementById("myLink").onclick = function() { document.getElementById("abc").href="xyz.php"; return false; }; </script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...