javascript - 尝试在 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');
但这不起作用。
解决方案
推荐阅读
- sql-server - 调试期间 SQL Server 远程服务器超时(在 IIS Express 上运行)
- node.js - 如何在 Node.js 中收听分离的未引用生成的子进程的标准输出?
- arrays - 带有数组的冒号运算符的奇怪行为
- c# - 如何从 .csproj 文件中获取引用?
- angular - 测试 Angular 材质数据源排序
- c++ - ceil、floor 和 round 转换为整数时会遇到精度损失问题吗?
- javascript - 运行 Promise 时如何更新 VUE 元素
- r - R中是否有一种方法可以从带有断点的向量创建一个具有某些间隔边界的字符向量?
- php - 显示没有外部来源的邮件内容(图像、css 等)
- excel - 插入从 Excel 到 Word 的范围作为大写