简体   繁体   English

如何获取两个属性的值并将其放在另一个属性中?

[英]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-datedate属性放到这个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.

相关问题 如何从内联样式属性中获取值并将其放入最接近的输入值中? jQuery的 - How can I get a value from an inline style attribute and put inside the closest input value? jQuery 如何在 namednodemap 中获取属性值 - How can i get attributes value in namednodemap 如何将两个函数放在一个onclick属性中? XSLT - how can i put two function in one onclick attribute? XSLT 如何获得 2 个输入的值,计算它们并将结果放入另一个 - How can I get the value of 2 inputs, calculate them and put the result in another 如何获取属性值的数量? - How can I get the number of the attribute value? 使用数据属性和jQuery,如何通过数据属性名称和数据属性值选择元素 - Using data attributes and jQuery how can I select elements by data attribute name AND data attribute value 如何在提示中将值放入此复选框的值? - How can I get the value in the prompt put it in the value of this checkbox? 如何使用Javascript将属性中的值获取到另一个属性 - How to get a value in an attribute to another attribute with Javascript 如何使用jQuery仅删除一个属性值而不能删除另一个? - How can I remove only one attribute value and not another with jQuery? 如何检查两个对象的属性是否相似,除了一个属性? - How can I check if two object's attributes are similar except for one attribute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM