简体   繁体   English

按类和自定义属性值查找元素(然后更新它)

[英]Find element by class and custom attribute value (then update it)

Using jQuery I want to add some HTML to a specific <td> whose attribute matches the value I have. 使用jQuery我想将一些HTML添加到特定的<td>其属性与我的值相匹配。

For example I have a value '2015-02-01', I want to find/search the <td> which had the attribute 'data-date="2015-02-01"' and append HTML to the matched value, which in this example is <td>1</td> 例如,我有一个值'2015-02-01',我想找到/搜索具有属性'data-date =“2015-02-01”'的<td>并将HTML附加到匹配的值,在这个例子中是<td>1</td>

<div class="fc-content-skeleton">
<table>
<thead>
<tr>
<td data-date="2015-02-01" class="fc-day-number">1</td>
<td data-date="2015-02-02" class="fc-day-number">2</td>
<td data-date="2015-02-03" class="fc-day-number">3</td>
<td data-date="2015-02-04" class="fc-day-number">4</td>
<td data-date="2015-02-05" class="fc-day-number">5</td>
<td data-date="2015-02-06" class="fc-day-number">6</td>
<td data-date="2015-02-07" class="fc-day-number">7</td>
</tr>
</thead>
</table>
</div>

I dont have any sample code as I am stuck really on how to approach this. 我没有任何示例代码,因为我真的陷入了如何处理这个问题。

// This only gives me the value of the attribute
$this.parent().find('.fc-day-number').attr('data-date');

Just use attribute selector whose syntax is [attributeName[=attributeValue]] 只需使用语法为[attributeName[=attributeValue]] 属性选择器

var value = "2015-02-01";
$('[data-date="' + value + '"]').append('html here');

Note, that you can apply the above selector in find , closest and other such jQuery methods that accept a CSS selector. 注意,您可以在findclosest和其他接受CSS选择器的jQuery方法中应用上面的选择器。

Use jQuery Attribute Equals selector 使用jQuery Attribute Equals选择器

then use jQuery html() to add content 然后使用jQuery html()添加内容

or use jQuery append() to append content 或者使用jQuery append()来追加内容

here is the html() fiddle 这是html() 小提琴

I think is that you want : 我想是你想要的:

var date = "2015-02-03";

$("td[data-date='" + date + "']").append(date);

Solution on this url : http://jsfiddle.net/g09jvfxy/1/ 这个网址的解决方案: http//jsfiddle.net/g09jvfxy/1/

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

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