English 中文(简体)
Pull javascript code from tag using js or jQuery
原标题:

How would I retrieve the javascript call in the anchor tag below in JS or JQuery? Basically I want to get the code ("javascript:do_my_function( blah , string1 , 1);") retrieved so I can execute it. This anchor is embedded several deep in some div tags as well.

<a onmouseout="swapImage( btn1 ,  , http://img2.comp.com/img/buttons/btn_g.png ,1)" onmousedown="swapImage( btn1 ,  , http://img2.comp.com/img/buttons/btn_g_d.png ,1)" onmouseover="swapImage( btn1 ,  , http://img2.comp.com/img/buttons/btn_g_a.png ,1)" href="javascript:do_my_function( blah ,  string1 , 1);">
<img id="btn1" width="180" height="60" alt="" src="http://img2.comp.com/img/buttons/btn_ge.png"/>
</a>
最佳回答

To retrieve it just use:

 var href = document.getElementById( btn1 ).parentNode.href;

That just finds the img by id, and grabs its parent, the a tag in question.

To call it, you can use:

 window.location = href;

Or you could parse it and eval() it. All of this is highly unconventional by the way. Do you not have any further control over the code?

问题回答

To do it with jQuery you can do

var jscript = $( #btn1 ).parent().attr( href );
eval(jscript);

This is not a recommended way to do things. Changing the html would be much better





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签