[英]jquery - Prevent onclick and retrieve parameters of function call
我们被要求对某些代码进行创可贴措施,因此这看起来确实很愚蠢,但请耐心等待。
我们需要防止锚的onclick方法,并检索要进行的函数调用的参数,以便使用这些参数调用其他函数。 这是到目前为止的代码和html:
<p><a href="#" onclick="openMoreProductVideos('Title of video','VideoCode','false');return false;"><span class="txt">View all videos</span></a></p>
JS:
// undefine onclick so it is not handled with broken function
$('a[onClick^=openMoreProductVideos]').each(function () {
this.onclick = undefined;
});
// grab parameters from onclick function call and submit to new function.
$('a[onClick^=openMoreProductVideos]').click(function () {
strStart = "openMoreProductVideos(";
strFinish = ");return false"
srctext = $(this).parent().html();
var re = strStart + ".*?"+strFinish
var newtext = srctext.match(re)[1];
var callparams = newtext.substring(1,newtext.length-1);
testparams(callparams);
});
// test function to see if parameters are working
function testparams (a,b,c){
console.log (a,b,c)
}
问题在于返回的字符串(callparams)被视为一个参数。 我不是正则表达式专家,所以我希望有一个比我更能快速看到解决方案的经验的人。
非常感谢。
如果callparams
是一个字符串,例如"'Title of video','VideoCode','false'"
则尝试
testparams(callparams.split(','));
/* transforming string into an array
* ["'Title of video'", "'VideoCode'", "'false'"]
*/
...
function testparams (pars){
console.log (pars[0], pars[1], pars[2]);
/* if necessary remove trailing quotes with .replace() method */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.