首页 > 解决方案 > 尝试在 attr() 中添加变量

问题描述

我有这段代码用于检查带有 url 参数的复选框。

var i = document.location.href.lastIndexOf('?');
var types = document.location.href.substr(i+1).replace(/editeur=/g,'').split('&');
$('input[name="editeur"]').prop('checked',function(){
     return $.inArray(this.value,types) !== -1;
});

这就是 HTML

<div class="hEditeur">
  <input id="edit" name="editeur" type="checkbox" value="edit">
  <label for="edit">
    Editeur 1
    <svg height="18" viewBox="0 0 31 31" width="18" xmlns="http://www.w3.org/2000/svg">
      <path d="M15.5.5v30m15-15H.5" stroke-linejoin="miter" stroke-width="3px"></path>
    </svg>
  </label>
</div>
<section id="domaine">
  <div>
    <input id="domaine1" name="domaine" type="checkbox" value="domaine1" data-editeur="edit">
    <label for="domaine1">Domaine 1</label>
  </div>
  <div>
    <input id="domaine2" name="domaine" type="checkbox" value="domaine2" data-editeur="edit">
    <label for="domaine2">Domaine 2</label>
  </div>
</section>

我现在要做的是在 data-editeur 的帮助下添加一个 class='open' 到 #domaine 以及我在开始时使用代码获得的变量。我尝试类似的东西

$('#domaine input').parent().parent().attr(''+types).addClass('open');

但这不起作用。

标签: javascriptjquery

解决方案


推荐阅读