[英]How can I get the value of two attributes and put it in another attribute?
I have a multiple calendar table and I want to combine all attribute in another attribute. 我有一个多日历表,我想在另一个属性中组合所有属性。 Is it possible or not?
有可能吗? Just experimenting.
只是试验。 Here's a sample code of the * HTML *
这是* HTML *的示例代码
<div class="day_num" data-date="17" date="July 2019">17</div>
I want to put the data-date
and date
attribute to this div attribute: 我想将
data-date
和date
属性放到这个div属性中:
<div full-date=""></div>
Is this possible? 这可能吗? sorry for experimenting such thing.
抱歉试验这样的事情。
* This is a sample calendar * *这是一个样本日历*
Every cell has the div
mention above. 每个单元都有上面提到的
div
。 I will include the HTML
output 我将包含
HTML
输出
This will loop through day_num elements, combine its attribute to form the date & assign that date to the child div into the full-date attribute. 这将循环遍历day_num元素,组合其属性以形成日期并将该日期分配给子div为完整日期属性。
$('.day_num').each(function(){
let full_date = $(this).attr('data-date')+' '+$(this).attr('date');
$(this).children('div').attr('full-date',full_date);
})
The attribute values are plain strings, which can be combined (concatenated) together as you want, using the +
operator. 属性值是纯字符串,可以使用
+
运算符将其组合(连接)在一起。 I assume you would want to put "17 July 2019" as the full-date attribute (assuming here your elements have and ID tags "source" and "target", to make sure the selector returns a single element): 我假设您希望将“2019年7月17日”作为完整日期属性(假设您的元素具有ID标签“source”和“target”,以确保选择器返回单个元素):
let day = $("#source").attr("data-date");
let date = $("#source").attr("date");
let fullDate = day + " " + date;
$("#target").attr("full-date", fullDate);
Edit: With the full HTML and table in the question; 编辑:在问题中使用完整的HTML和表格; You'll need to loop over the elements
$(".day_num")
, and apply the same piece of code to each fragment: 您需要遍历元素
$(".day_num")
,并将相同的代码应用于每个片段:
$(".day_num").each(function () {
let $this = $(this);
let day = $this.attr("data-date");
let date = $this.attr("date");
let fullDate = day + " " + date;
$this.find("div").attr("full-date", fullDate);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.