简体   繁体   English

如何解析属性onclick,其中包含返回函数。 javascript

[英]how to parse attribute onclick, which contains return function. javascript

I have a link which return form with different ID's, and i need to get ID number from onclick attribute 我有一个返回带有不同ID的表单的链接,我需要从onclick属性获取ID号

<a onclick="return package_order(14400287396);" href="javascript:;">advanced</a>
<a>doit</a>

this jquery code: 这个jQuery代码:

 var pack = $('a:contains("advanced")').attr("onclick");
 $('a:contains("doit")').click(function(){
    alert(pack)

and i get: 我得到:

function onclick(event) {
  return package_order(14400287396);
}

but i need only id number inside() should i use regular expression to do it? 但我只需要ID号inside()就可以使用正则表达式来做到这一点? or what? 要不然是啥?

onclick属性中提取的“ id”是这样的:

$('a:contains("advanced")').attr("onclick").match(/\d+(?=\))/)[0]

you could do 你可以做

var idStr = pack.split('(')[1];
var id = idStr.substring(0, idStr.length-2);

which will split that string into an array based on the first left paren, and then take the second array value, then subtract the last 2 chars 它将根据第一个左括号将字符串拆分成一个数组,然后获取第二个数组值,然后减去最后两个字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用包含函数的 JSON-Object 进行函数调用。 如何获取函数外的返回变量? - function call with JSON-Object, which contains a function. how to get return variable outside the function? 如何回显mysqli导致div具有javascript函数的onclick,其中包含另一个onclick用于不同的javascript函数 - How to echo mysqli results in a div with an onclick for javascript function which contains another onclick for different javascript function 如何返回onclick函数是javascript中href的结果? - How to return the onclick function is result to href in javascript? Javascript onclick函数。 输入块。 预期封锁 - Javascript onclick function. Input block. Expected block Javascript-接受参数并返回包含“ x”的字符串的函数 - Javascript - A function that accepts parameter and return a string which contains “x” 如何在 onclick 属性中返回异步函数的值? - How to return an async function's value in onclick attribute? jQuery onclick缩略图功能。 - Jquery onclick thumbnails function. 如何在JavaScript函数中处理arraylist? - How to handle arraylist in javascript function.? javascript等待返回函数转换为onclick - javascript wait return function into onclick 哪个代码更有效? (调用javascript函数。) - Which code is more efficient? (calling javascript function.)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM