简体   繁体   中英

Parameter from <a href></a> to javascript

I have a <ul> <li> menu. 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. How can I do it? How can i send/retrieve parameters?

Example Javascript:

function addTab(title, uri) {
    var tabNameExists = false;
    $('#tabs ul li a').each(function(i) {
         if (this.text == title) {
             tabNameExists = true;
         }
    });

Example 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) .

Note: If you want to fetch the attribute of the <a> or <li> tag , you can go with @PSR's answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM