简体   繁体   中英

html anchor tag reference

i have an anchor tag as below.

<a style="border:0px" href='javascript:deleteAttachment(this);' />

Inside the deleteAttachment, how can i get the anchor tag. Sending this to the method, sends the window element to the method.

function deleteAttachment(ancElement){
    //Jquery operation on acnElement
}

Please helop me out.

I would recommend a slightly different approach, since what you're trying to do is a bit old. assuming you already loaded jQuery, here we go:

<a id="myFirstLink" href="someHref" />
<a class="otherLinks" href="secondHref" />
<a class="otherLinks" href="thirdHref" />

<script>
$(function() {
     $('#myFirstLink, .otherLinks').click( function(event) {
         // stops the browser from following the link like it would normally would
         event.preventDefault(); 
         // do something with your href value for example
         alert( $(this).attr('href') );
     });
});
</script>

So basically what you can do is this: simply generate all your anchors like you would normally would and apply the same class name to each of them - in my example the class would be "otherLinks".
After that, all your links will be handled by that anonymous function.

Use the onclick handler:

<a onclick="deleteAttachment(this)">

or, the cleanest and most accepted method nowadays, have just the raw link in the HTML:

<a id="deleteAttachment">

and add the click event programmatically, in a separate script block, on DOM load:

 document.getElementByID("deleteAttachment").onclick = 
    function() {  ... you can use "this" here ....  }

you must set its ID attribute

  <a id="myAnchor" style="border:0px;" href="javascript:deleteAttachment('myAnchor');"/>

then use jquery to find it

function deleteAttachment(ID)
{
   var MyAnchor =  $('#'+ID);
}

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