jQuery - 从属性获取文本

jQuery - getting text from attribute

我正在使用一个名为 pickadate 的插件,我正在尝试从所选日期获取 aria 标签文本并将其应用到 div。我能够一次检索该值,但如果您在另一天单击,#date 中的日期不会更新?

http://jsfiddle.net/mg6fxqbe/

 var $elements = {
     datepicker: $('.js-datepicker'),
 };

 $elements.datepicker.pickadate();


 $('.picker__day').on('click', function () {
     var date = $(this).attr('aria-label');
     $('#date').empty().text(date);
 });

试试这个:

 var $elements = {
     datepicker: $('.js-datepicker'),
 };

$elements.datepicker.pickadate();


 $('.picker__day').on('click', function (e) {
     $('#date').empty();
     var date = $(e.target).attr('aria-label');
     $("#date").html(date);
     return false;
 });

Updated fiddle

如日期选择器插件文档中所述,您可以使用 onSet 事件来检索已设置的当前日期。

The onSet event is the only callback that is passed a context argument that provides details as to which properties are being “set”.

Within scope of all six of these events, this refers to the picker.

$('.js-datepicker').pickadate({
    onSet: function() {
        $('#date').text(this.$node.val());
    }
});
#date{ font-weight:bold; font-size:40px; margin:20px 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://amsul.ca/pickadate.js/vendor/pickadate/lib/picker.js"></script>
<script src="http://amsul.ca/pickadate.js/vendor/pickadate/lib/picker.date.js"></script>
<input class="js-datepicker" type="text" placeholder="Select date" />
<div id="date"></div>

Here 您可以找到更多相关信息。