如何将变量插入jquery select属性$("[myAttribute ='+ myAttribute +']").html('is working');

Ben*_*Ben 0 javascript jquery

我想用我自己的属性选择和更改一个div让我说myAttribute ="1235"

我试试这个

<font myAttribute="1235">Change me !</font>
<script type="text/javascript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
    var myAttribute = 1235;                    
      $("[myAttribute='+myAttribute+']").html('is working');                    
      $("[myAttribute='+myAttribute']").html('is working');                    
      $("[myAttribute=''+myAttribute]").html('is working');                
</script>
Run Code Online (Sandbox Code Playgroud)

但他们不工作!如果将代码更改为

$("[myAttribute='1235']").html('is working');
Run Code Online (Sandbox Code Playgroud)

everthing还可以,但我想使用变量

Ren*_*Pot 5

你需要一个突出显示的编辑器:)

添加额外的报价以实现它

$("[myAttribute='"+myAttribute+"']")
Run Code Online (Sandbox Code Playgroud)