Html 使用 JavaScript 获取 href 值并打开新窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18524407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a href value using JavaScript and open it new window
提问by Leonel Sarmiento
What I want to do is to get the value of value
and open it in new window with a specific size using JavaScript.
我想要做的是value
使用 JavaScript获取 的值并在具有特定大小的新窗口中打开它。
HTML
HTML
<a id="LNK" href="##" value="edit2.aspx?ren=<%# Eval("GLR_ID") %>" onclick="clickLink()">RENAME</a>
JavaScript
JavaScript
<script type="text/javascript">
function clickLink() {
var myLink = //I need to get the value of `value`
window.open(myLink,'Rename','height=150px','width=250px');
return false;
}
</script>
Please correct my grammar, Thank You.
请纠正我的语法,谢谢。
回答by aspie
document.getElementById("LNK").getAttribute("value");
回答by Trevor Dixon
http://jsfiddle.net/trevordixon/4HE97/
http://jsfiddle.net/trevordixon/4HE97/
Pass a reference to the anchor to clickLink
:
将锚点的引用传递给clickLink
:
<a id="LNK" href="##" value="edit2.aspx?ren=<%# Eval("GLR_ID") %>" onclick="clickLink(this)">RENAME</a>
Use getAttribute
to get the value:
使用getAttribute
来获取值:
function clickLink(a) {
var myLink = a.getAttribute('value');
window.open(myLink,'Rename','height=150px','width=250px');
return false;
}
回答by Suresh Pattu
Try this
尝试这个
window.clickLink = function(a) {
var myLink = a.getAttribute('value');
window.open(myLink,'Rename','height=150px','width=250px');
return false;
}
回答by arb
Check out this solution:
看看这个解决方案:
function clickLink(e) {
var myLink = e.getAttribute("value");
window.open(myLink,'Rename','height=150px','width=250px');
return false;
}
HTML:<a id="LNK" href="##" value="edit2" onclick="clickLink(this)">RENAME</a>
HTML:<a id="LNK" href="##" value="edit2" onclick="clickLink(this)">RENAME</a>
A working example can be found here
可以在此处找到一个工作示例
回答by Dipesh Parmar
You can get DOM value using getElementByID().value
您可以使用获取 DOM 值 getElementByID().value
document.getElementByID('LNK').value
OR
或者
You can pass argument into your onClick
function.
您可以将参数传递给您的onClick
函数。
onclick="clickLink(this.value)"
and in function
.
并在function
.
function clickLink(value) {
var myLink = value;