Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery可以从字符串变量中的HTML标记获取属性吗_Javascript_Jquery_Substring - Fatal编程技术网

Javascript jQuery可以从字符串变量中的HTML标记获取属性吗

Javascript jQuery可以从字符串变量中的HTML标记获取属性吗,javascript,jquery,substring,Javascript,Jquery,Substring,在JavaScript内部,在运行时,我在字符串中获得复选框的HTML代码,如下所示: var chk="<input type='checkbox' id='checkbox1' name='chk1' />"; var chk="<input type='checkbox' id='checkbox1' name='chk1' />"; id = $(chk).attr('id'); alert(id); //alerts 'checkbox1' 最后,s包含复选

在JavaScript内部,在运行时,我在
字符串中获得
复选框
的HTML代码,如下所示:

var chk="<input type='checkbox' id='checkbox1' name='chk1' />";
var chk="<input type='checkbox' id='checkbox1' name='chk1' />";
id = $(chk).attr('id');
alert(id); //alerts 'checkbox1'
最后,
s
包含
复选框的
id

试试这个

var chk = $("<input type='checkbox' id='checkbox1' name='chk1' />");

alert(chk.attr('id'));​
var chk=$(“”);
警报(chk.attr('id'));​

是的,您可以通过jquery轻松完成,如下所示:

var chk="<input type='checkbox' id='checkbox1' name='chk1' />";
var chk="<input type='checkbox' id='checkbox1' name='chk1' />";
id = $(chk).attr('id');
alert(id); //alerts 'checkbox1'
var chk=”“;
id=$(chk.attr('id');
警报(id)//警报“复选框1”

请参见“工作”

@kayen是的,这是另一种方法,感谢您指出:-)