jquery - 设置属性不起作用

标签 jquery html

我有 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/

相关文章:

html - Bootstrap 水平表单元素有 -15px 边距

html - 如何使 .html.erb 文件在 Rails 之外工作?

javascript - 带有 json 的 Jquery 自动完成失败

javascript - jQuery CSS 表情崩溃

javascript - 使用 jquery 的点击事件进行分页

html - Bootstrap 3 选项卡无法正确切换事件状态

html - 边框和带有边框半径的子元素之间的间隙

javascript - 如何将图像从一个 html 页面更改为另一个?

javascript - daterangepicker - 将字段输入复制到另一个字段输入

javascript - 如何使用每列单独的滚动条创建两列布局