[英]Javascript function to send parameter
Please tell me how to send div id as a parameter to javascript function onclick in a link of a page to open it via javascript .请告诉我如何将 div id 作为参数发送到页面链接中的javascript函数 onclick 以通过javascript打开它。 I want to send div id to open that particular link.
我想发送 div id 来打开那个特定的链接。
<a id="anchor22" href="http://www.friferie.dk/inspiration/%C3%98strig" onclick="MyFunction()>Ostrig</a>
<script type="text/javascript">
function MyFunction(anchor) {
document.getElementById('achor').click();
}
</script>
Use this way -用这种方式——
function myFunction(x) { document.getElementById('showId').innerHTML = x.parentElement.id; // x.parentElement.id gets the parent div then its id value }
<div id="div1"> <a href="#" onclick="myFunction(this)">Click Me</a> <!-- pass this to the function --> </div> <br />Div's id: <span id="showId"> </span>
The above code was just for simplicity.上面的代码只是为了简单起见。 For your sample code -
对于您的示例代码 -
<div id="div1">
<a id="anchor22" href="http://www.friferie.dk/inspiration/%C3%98strig" onclick="MyFunction(this)>Ostrig</a>
</div>
<script type="text/javascript">
function MyFunction(anchor) {
alert(anchor.parentElement.id); // Will return div1
alert(anchor.id); // Will return anchor22
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.