简体   繁体   English

如何使用jQuery通过数据日期属性选择元素?

[英]How to select element by data-date attribute with jQuery?

I've tried to create a code snippet for an example of a selector I was trying to use and it's not working.我试图为我尝试使用的选择器示例创建一个代码片段,但它不起作用。 Can someone eyeball it and tell me what I have wrong?有人可以观察它并告诉我我有什么问题吗?

 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>

My code was inspired by this example: http://api.jqueryui.com/data-selector/我的代码受到这个例子的启发: http : //api.jqueryui.com/data-selector/

You want an attribute selector:你想要一个属性选择器:

$("[data-date]")

Fiddle: https://jsfiddle.net/j3cmo4ow/5/小提琴: https : //jsfiddle.net/j3cmo4ow/5/

If you want the :data pseudo selector to work, you need to include jQuery UI.如果你想让:data伪选择器工作,你需要包含 jQuery UI。

$("[data-color]").each(function() {
  var element = $(this);
  element.css("backgroundColor", element.attr('data-color'));
});

Fiddle: https://jsfiddle.net/j3cmo4ow/8/小提琴: https : //jsfiddle.net/j3cmo4ow/8/

Your issue with selector $(":data(date)") you can select all elements with date Data using this selector : $("*[data-date]")您的选择器问题$(":data(date)")您可以使用此选择器选择带有date Data所有元素: $("*[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>

This fails, because the jQueryUI library is invoked over http, while the fiddle itself is loaded via https.这失败了,因为 jQueryUI 库是通过 http 调用的,而小提琴本身是通过 https 加载的。

Therefore the script is considered as insecure.因此该脚本被认为是不安全的。

When you run your fiddle with the console open you see the following error:当您在控制台打开的情况下运行您的小提琴时,您会看到以下错误:

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.

The :data() selector does not work based off of HTML attributes with the prefix data- . :data()选择器不能基于带有前缀data-的 HTML 属性工作。

Instead, it matches and elements that have data stored via the jQuery function .data( "foo", value ) .相反,它匹配通过 jQuery 函数.data( "foo", value )存储数据的元素。

请注意, :data()选择器是 jQuery UI 的一部分,而不是核心 jQuery 库的一部分,因此除非您包含 jQuery UI,否则该选择器不会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM