简体   繁体   中英

jQuery for scraping URL from within “onclick” attribute of link

I am using podPress on my Wordpress instance to give users some playback controls in listening to our podcast episodes. Below, I have the code found on the play button of these controls.

What I'd like to do is write some jQuery that can scrape out the URL of the podcast episode. In this test case, that URL is http://www.test.com/episodes/2012_04_11_episode.mp3 . I'd like to write this URL to a variable and then use it elsewhere on my site where I'm building an HTML5 audio player that features our latest episode in a more central location.

Based on the way podPress writes these players, the ID of the link with the URL that I'll need will ALWAYS be "podpress_html5_play_1". I know how to target this ID, but I don't know how to grab a string that is nested within the "onclick" element. Can anyone help?

Here is the code found on the link:

<a id="podpress_html5_play_1" href="javascript:void(null);" onclick="podPressenprintHTML5audio('1', 'http://www.test.com/episodes/2012_04_11_episode.mp3', true);" class="podpress_play_button" title="Play &gt;" style="background-image:url(http://www.test.com/beta/wp-content/plugins/podpress/images/play_button_dyn_v4_32.png);"></a>
var link = 'http'+$('#podpress_html5_play_1').attr('onclick').split('http')[1].split("'")[0];

小提琴

尝试这个:

var link = $("#podpress_html5_play_1").attr("onclick");

Just Do this: Assuming you are using jQuery;

 // Get the onclick attribute

    var onclickAttr = $('a#podpress_html5_play_1').attr('onclick');

// strip and split it into an array with the single quote string

    var a  = onclickAttr.split("'");

// Now a[3] holds your link

alert (a[3]);

JSFiddle: http://jsfiddle.net/P6Kzt/

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