[英]Parameter from <a href></a> to javascript
I have a <ul>
<li>
menu. 我有一个<ul>
<li>
菜单。 When the <a>
element inside the <li>
is clicked, I need to run a Javascript function and this function needs some parameters like id or name. 单击<li>
的<a>
元素时,我需要运行Javascript函数,并且此函数需要一些参数,例如id或name。 How can I do it? 我该怎么做? How can i send/retrieve parameters? 如何发送/检索参数?
Example Javascript: 示例Javascript:
function addTab(title, uri) {
var tabNameExists = false;
$('#tabs ul li a').each(function(i) {
if (this.text == title) {
tabNameExists = true;
}
});
Example HTML: HTML示例:
<ul>
<li><a href="#">W to set parameters</a>
</ul>
<a href="url" onClick="addTab(this);">link</a>
function addTab(this)
{
$(this).attr('id');
$(this).attr('name');
}
尝试
<a href="url" id="someUniqueID" title="SomeTitle" onClick = "addTab(this.title, this.href,this.id);" >link</a>
Your codes are not quite related to your question, anyway, here is the way: 无论如何,您的代码与您的问题不太相关,方法如下:
<li><a href="#" onclick="some_js_function(id,name)">W to set parameters</a></li>
More logics can be made, depends on how you write some_js_function(id,name)
. 可以编写更多逻辑,具体取决于您如何编写some_js_function(id,name)
。
Note: If you want to fetch the attribute of the <a>
or <li>
tag , you can go with @PSR's answer. 注意:如果要获取<a>
或<li>
标记的属性,则可以使用@PSR的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.