简体   繁体   English

jQuery 如何通过属性和值获取元素是变量?

[英]jQuery how to get element by attribute and value is an variable?

I defined a variable comment_id , it is the value of comment_id attribute of a form tag:我定义了一个变量comment_id ,它是表单标签的comment_id属性的值:

var comment_id = '44';

so can I insert the value of the variable into the following:所以我可以将变量的值插入以下内容:

$( "div[comment_id = comment_id ]" )

so that I can locate the element that.这样我就可以找到那个元素。 with the attribute value.与属性值。

I tried something like:我试过类似的东西:

$( "div[comment_id = $comment_id ]" )

but not work.Any friend can help,I'm a Python guy and I'm very frustrated by JS it is very complicated and need code lots of code to do simple function.但不行。任何朋友都可以帮忙,我是 Python 的人,我对 JS 感到非常沮丧,它非常复杂,需要大量代码来完成简单的 function。

try using the backtick character instead of the double quote尝试使用反引号字符而不是双引号

$(`div[comment_id = ${comment_id} ]`)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM