我有以下渠道:
<div id="panel">
<pre class="channel" id="one">ONE</pre>
<pre class="channel" id="two">TWO</pre>
</div>
我想以这种方式用属性标记频道:
function onSelectChannel()
{
// callback
var foundChannel = $(this);
var prevSelectedChannel = $("[selected='true']");
if (foundChannel.size() > 0) {
foundChannel.css('background-color', selectedChannelColor);
foundChannel.prop("selected", "true");
if (prevSelectedChannel.size() > 0){
prevSelectedChannel.css('background-color', channelsColor);
prevSelectedChannel.prop("selected", "false");
}
}
}
我可以找到foundChannel并设置属性(可以在调试器中看到它),但是找不到具有已设置属性的对象。jQuery不返回任何内容。
更新:我想用一个标记(可以有任何名称)标记某些元素(不相关),我的意思是:为属性或属性设置任何值-我不在乎,并且可以随时找到该元素。这只是我想要的。
如果要使用.prop()
jQuery的方法更新属性,则需要过滤一组元素。在这种情况下,不能使用属性选择器。
function onSelectChannel() {
// callback
var foundChannel = $(this);
var prevSelectedChannel = $(".channel").not(this).filter(function () {
return this.selected
});
if (foundChannel.length > 0) {
foundChannel.css('background-color', selectedChannelColor);
foundChannel.prop("selected", true);
if (prevSelectedChannel.size() > 0) {
prevSelectedChannel.css('background-color', channelsColor);
prevSelectedChannel.prop("selected", false);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句