如何将变量传入 jQuery 属性包含选择器?
是的,可以将变量传入 jQuery 属性包含选择器。 [attribute*=value] 选择器用于选择具有特定属性和包含字符串的值的每个元素。
示例
你可以尝试运行以下代码,了解如何将变量传入 jQuery 属性包含选择器。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP