如何将变量传递给 jQuery 的 `attribute-contains` 选择器?
jqueryweb developmentfront end technology
是的,可以将变量传递给 jQuery 的 `attribute-contains` 选择器。`[attribute*=value]` 选择器用于选择具有特定属性且值为字符串的元素。
示例
您可以尝试运行以下代码,了解如何将变量传递给 jQuery 的 `attribute-contains` 选择器:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function() {
var testString = "vp-485858383";
$('[data-vp*="id: ' + testString + '"]').css('color', 'blue');
});
</script>
</head>
<body>
<div data-vp='{
id: vp-485858383,
customTwo: "test"
}'>
Hello World
</div>
</body>
</html>

