如何使用 jQuery 按数据日期属性 select 元素?
How to select element by data-date attribute with jQuery?
我尝试为我尝试使用的选择器示例创建一个代码片段,但它不起作用。有人可以观察一下并告诉我我做错了什么吗?
var dateDiv = null;
var expenseDate = "06/22/2016";
$(":data(date)").each(function() {
var element = $(this);
element.css("backgroundColor", element.data("color"));
});
.expense-item {
margin-left: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="contentWrapper">
<div data-date="06/22/2016" data-color="red">
06/22/2016
<div class="expense-body">
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (3)
</p>
</div>
</div>
</div>
<div data-expense-date="06/23/2016" data-color="blue">
<div class="expense-body">
06/23/2016
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
</div>
</div>
<div data-expense-date="06/24/2016" data-color="yellow">
<div class="expense-body">
06/24/2016
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (3)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (4)
</p>
</div>
</div>
</div>
</div>
我的代码受到了这个例子的启发:
http://api.jqueryui.com/data-selector/
您需要一个属性选择器:
$("[data-date]")
Fiddle: https://jsfiddle.net/j3cmo4ow/5/
如果你想让:data
伪选择器起作用,你需要包含jQueryUI.
$("[data-color]").each(function() {
var element = $(this);
element.css("backgroundColor", element.attr('data-color'));
});
Fiddle: https://jsfiddle.net/j3cmo4ow/8/
这失败了,因为 jQueryUI 库是通过 http 调用的,而 fiddle 本身是通过 https 加载的。
因此脚本被认为是不安全的。
当您 运行 您的 fiddle 打开控制台时,您会看到以下错误:
Mixed Content: The page at 'https://jsfiddle.net/j3cmo4ow/4/' was loaded over HTTPS, but requested an insecure script 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.js'. This request has been blocked; the content must be served over HTTPS.
您对 select 或 $(":data(date)")
的问题
您可以 select 所有具有 date
Data
的元素使用此 select 或:$("*[data-date]")
var dateDiv = null;
var expenseDate = "06/22/2016";
$("*[data-date]").each(function() {
var element = $(this);
element.css("background-color", element.attr('data-color'));
});
var dateDiv = null;
var expenseDate = "06/22/2016";
$("*[data-date]").each(function() {
var element = $(this);
element.css("backgroundColor", element.data("color")); // Also works if you want.
});
.expense-item {
margin-left: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="contentWrapper">
<div data-date="06/22/2016" data-color="red">
06/22/2016
<div class="expense-body">
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (3)
</p>
</div>
</div>
</div>
<div data-expense-date="06/23/2016" data-color="blue">
<div class="expense-body">
06/23/2016
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
</div>
</div>
<div data-expense-date="06/24/2016" data-color="yellow">
<div class="expense-body">
06/24/2016
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (3)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (4)
</p>
</div>
</div>
</div>
</div>
:data()
选择器无法基于前缀为 data-
的 HTML 属性工作。
相反,它匹配通过 jQuery 函数存储数据的元素 .data( "foo", value )
。
注意,:data()
选择器是 jQuery UI 的一部分,而不是核心 jQuery 库的一部分,所以除非你包含 jQuery UI,该选择器预计不会工作。
我尝试为我尝试使用的选择器示例创建一个代码片段,但它不起作用。有人可以观察一下并告诉我我做错了什么吗?
var dateDiv = null;
var expenseDate = "06/22/2016";
$(":data(date)").each(function() {
var element = $(this);
element.css("backgroundColor", element.data("color"));
});
.expense-item {
margin-left: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="contentWrapper">
<div data-date="06/22/2016" data-color="red">
06/22/2016
<div class="expense-body">
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (3)
</p>
</div>
</div>
</div>
<div data-expense-date="06/23/2016" data-color="blue">
<div class="expense-body">
06/23/2016
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
</div>
</div>
<div data-expense-date="06/24/2016" data-color="yellow">
<div class="expense-body">
06/24/2016
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (3)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (4)
</p>
</div>
</div>
</div>
</div>
我的代码受到了这个例子的启发: http://api.jqueryui.com/data-selector/
您需要一个属性选择器:
$("[data-date]")
Fiddle: https://jsfiddle.net/j3cmo4ow/5/
如果你想让:data
伪选择器起作用,你需要包含jQueryUI.
$("[data-color]").each(function() {
var element = $(this);
element.css("backgroundColor", element.attr('data-color'));
});
Fiddle: https://jsfiddle.net/j3cmo4ow/8/
这失败了,因为 jQueryUI 库是通过 http 调用的,而 fiddle 本身是通过 https 加载的。
因此脚本被认为是不安全的。
当您 运行 您的 fiddle 打开控制台时,您会看到以下错误:
Mixed Content: The page at 'https://jsfiddle.net/j3cmo4ow/4/' was loaded over HTTPS, but requested an insecure script 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.js'. This request has been blocked; the content must be served over HTTPS.
您对 select 或 $(":data(date)")
的问题
您可以 select 所有具有 date
Data
的元素使用此 select 或:$("*[data-date]")
var dateDiv = null;
var expenseDate = "06/22/2016";
$("*[data-date]").each(function() {
var element = $(this);
element.css("background-color", element.attr('data-color'));
});
var dateDiv = null;
var expenseDate = "06/22/2016";
$("*[data-date]").each(function() {
var element = $(this);
element.css("backgroundColor", element.data("color")); // Also works if you want.
});
.expense-item {
margin-left: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="contentWrapper">
<div data-date="06/22/2016" data-color="red">
06/22/2016
<div class="expense-body">
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (3)
</p>
</div>
</div>
</div>
<div data-expense-date="06/23/2016" data-color="blue">
<div class="expense-body">
06/23/2016
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
</div>
</div>
<div data-expense-date="06/24/2016" data-color="yellow">
<div class="expense-body">
06/24/2016
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (3)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (4)
</p>
</div>
</div>
</div>
</div>
:data()
选择器无法基于前缀为 data-
的 HTML 属性工作。
相反,它匹配通过 jQuery 函数存储数据的元素 .data( "foo", value )
。
注意,:data()
选择器是 jQuery UI 的一部分,而不是核心 jQuery 库的一部分,所以除非你包含 jQuery UI,该选择器预计不会工作。