我有 channel :
<div id="panel">
<pre class="channel" id="one">ONE</pre>
<pre class="channel" id="two">TWO</pre>
</div>
我想用这种方式标记一个带有属性的 channel :
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 方法更新属性,则需要过滤元素集。在这种情况下您不能使用属性选择器。
--DEMO--
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);
}
}
}
关于jquery - 设置属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23809453/