Javascript:如何在不使用getElementById的情况下更改子元素的获取和更改属性
Javascript: How to change get and change attributes of child elements WITHOUT using getElementById
我正在尝试使用 Javascript 更改动态生成的值列表的单击行的颜色。问题是,所有行都具有相同的"类"属性(tr.odd 或 tr.even,"偶数"或"奇数"以使行之间有一些 css 差异......),我不想使用 ID。因此,要更改单击链接的颜色,我想获取(THIS)tr元素的任何属性,并更新(添加到其中)诸如"单击"之类的值。例如,每次我在标题属性中都有一个不同的 serialNr,我可以将"单击"添加到它作为标签,它看起来像"LG 1234 单击"。我不明白..我现在正在获取所有采用此标签的 tr 元素,因此在我单击一行后,每次在另一行的"mouseleave"上,虽然没有单击,但它的颜色在单击时会更改为蓝色,我认为这个问题是由于标签"单击"添加到所有 tr 元素的"title"属性中,而不是特别需要/我单击的那个。
下面是脚本代码:
$(document).on("mouseenter", ".filter-table tbody tr", function() {
$(this).css("background", "#21374C");
$(this).css("color", "#DFDFDF");
});
$(document).on("click", ".filter-table tbody tr", function() {
var element = document.getElementsByTagName("tr")[0].setAttribute("title", "clicked");
$(this).css("color", "blue");
$(this).css("background", "#DFDFDF");
console.log("attribute title is: " + element.getAttribute("title"));
});
$(document).on("mouseleave", ".filter-table tbody tr.odd", function() {
var element = document.getElementsByTagName("tr")[0];
if (element.getAttribute("title") == "clicked") {
$(this).css("color", "blue");
$(this).css("background", "#DFDFDF");
}
else {
$(this).css("background", "#DFDFDF");
$(this).css("color", "#222222");
}
});
$(document).on("mouseleave", ".filter-table tbody tr.even", function() {
var element = document.getElementsByTagName("tr")[0];
if (element.getAttribute("title") == "clicked") {
$(this).css("color", "blue");
$(this).css("background", "#DFDFDF");
}
else {
$(this).css("background", "#FFFFFF");
$(this).css("color", "#222222");
}
});
HTML 的示例如下:
<tr wicket:id="content-list" class="even" style="background-color: rgb(255, 255, 255); color: rgb(34, 34, 34); background-position: initial initial; background-repeat: initial initial;">
<td wicket:id="cells" title="SNr: 1"><span wicket:id="cell">1</span></td>
<td wicket:id="cells" title="DC-MAN: AGR"><span wicket:id="cell">AGR</span></td>
<td wicket:id="cells" title="DC-FOA: SS - MH"><span wicket:id="cell">SS - MH</span></td>
<td wicket:id="cells" title="DC-SC: ABCDEFGH"><span wicket:id="cell">ABCDEFGH</span></td>
</tr>
<tr wicket:id="content-list" class="odd" style="background-color: rgb(255, 255, 255); color: rgb(34, 34, 34); background-position: initial initial; background-repeat: initial initial;">
<td wicket:id="cells" title="SNr: 2"><span wicket:id="cell">2</span></td>
<td wicket:id="cells" title="DC-MAN: PPT"><span wicket:id="cell">PPT</span></td>
<td wicket:id="cells" title="DC-FOA: DB - OP"><span wicket:id="cell">DB - OP</span></td>
<td wicket:id="cells" title="DC-SC: MNBVCXY"><span wicket:id="cell">MNBVCXY</span></td>
</tr>
<tr wicket:id="content-list" class="even" style="background-color: rgb(255, 255, 255); color: rgb(34, 34, 34); background-position: initial initial; background-repeat: initial initial;">
<td wicket:id="cells" title="SNr: 3"><span wicket:id="cell">3</span></td>
<td wicket:id="cells" title="DC-MAN: QWE"><span wicket:id="cell">QWE</span></td>
<td wicket:id="cells" title="DC-FOA: AS - HG"><span wicket:id="cell">AS - HG</span></td>
<td wicket:id="cells" title="DC-SC: LKJHGF"><span wicket:id="cell">LKJHGF</span></td>
</tr>
你已经在起诉JQuery选择器了,为什么不在这里使用它呢?
例如:
var element = $("#idName")[0].setAttribute("title", "clicked");
或
var element = $(".className")[0].setAttribute("title", "clicked");
来自保罗克的BR
您应该删除内联样式并改为分配类。
$('td').click(function(){
$(this).parent().removeClass('hover').addClass('click');
}).mouseenter(function(){
$(this).parent().addClass('hover');
}).mouseleave(function(){
$(this).parent().removeClass('hover click');
});
演示
当然,我还没有浏览您的整个代码。您将需要修改要在 css 中应用的其他更改,我只是向您展示方法。祝你好运!
相关文章:
- 在不使用JQuery的情况下隐藏DOM中的选定元素
- 如何在不传递此信息的情况下查找被调用的元素
- JavaScript-在手机上不工作的情况下,在外部单击时隐藏元素
- 如何在不影响其他元素的情况下扩展DIV
- 在不移动内部文本的情况下缩放元素的效果
- 如何使用JavaScript在没有html dom的情况下隐藏html元素
- 如何在没有回调的情况下获取有关元素的信息
- Ajax:只在元素存在的情况下调用Ajax
- 是否可以在不重新渲染的情况下显示/隐藏父对象中的元素
- jQuery-如何在给定延迟的情况下从元素中删除类
- 在不引用文档的情况下使用AngularJS获取元素
- JavaScript-在不影响索引的情况下将元素插入数组
- 如何在不使用multipleparent()调用的情况下找到(元素表的)第一个祖先
- 是否可以在不创建svg对象的情况下创建捕捉元素?[snap.svg]
- 在没有大量回调函数的情况下在列表元素上循环播放同一动画
- 在不使用sort()的情况下获取HTML LI元素的副本
- 如何在不使用任何 html 输入/搜索元素的情况下读取本地客户端文本文件
- 如何在不丢失格式的情况下连续淡入() 元素文本的每个字符,包括嵌套元素
- 如何在不创建新元素的情况下修改 DOM 元素的位置
- 在不使用 CSS 的情况下,将元素与浏览器屏幕顶部保持设定的距离