jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从jquery中的属性开始,在遍历dom的系列中更改单个开/关开关的状态大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在jQuery中的一系列开/关开关有问题.

这是交换机的标记

<div class="switchOnOff">
    <div class="switchLed"></div>
    <p class="switch@L_262_1@BACk"></p>
    <a class="switchButton" rel="on" id="one"><span></span></a>
</div>

<div class="switchOnOff">
    <div class="switchLed"></div>
    <p class="switch@L_262_1@BACk"></p>
    <a class="switchButton" rel="on" id="two"><span></span></a>
</div>

这是我的js:

$('.switchOnOff .switchButton').click(function() {  
    if ( $('.switchButton').attr('rel') == 'off' ) {
        $(this).next('.switchLed').addClass("on");
        $(this).prev('.switch@L_262_1@BACk').html("on");
}

   else if ( $('.switchButton').attr('rel') == 'on' ) {
    $(this).next('.switchLed').addClass("off");
    $(this).prev('.switch@L_262_1@BACk').html(@L_262_1@BACkOn);

}});

但是这段代码不起作用.我也尝试使用“next”,“prev”或“最接近”的选择器,但DOM遍历不起作用.
总之,我希望获得一个可以改变单个开关状态的开/关功能.

我希望我很清楚.

任何帮助都会得到充分的帮助.

解决方法

你可以做(​​评论后更新)

$('.switchOnOff .switchButton').click(function() {
    var div = $(this).closest('.switchOnOff');
    var $this = $(this);
    if ($this.attr('rel') == 'off') {
        $this.attr('rel','on');
        div.find('.switchLed').addClass("on").removeClass("off");
        div.find('.switch@L_262_1@BACk').html("on");
    } else if ($this.attr('rel') == 'on') {
        $this.attr('rel','off');
        div.find('.switchLed').addClass("off").removeClass("on");
        div.find('.switch@L_262_1@BACk').html("off");


    }
});

在这里摆弄http://jsfiddle.net/YpfmD/3/

编辑最后评论:设置你可以做的“第一关”:

var switcher = function(el) {
    var div = $(el).closest('.switchOnOff');
    var $this = $(el);
    if ($this.attr('rel') == 'off') {
        $this.attr('rel','on');
        div.find('switchLed').addClass("on").removeClass("off");
        div.find('.switch@L_262_1@BACk').html("on");
    } else if ($this.attr('rel') == 'on') {
        $this.attr('rel','off');
        div.find('switchLed').addClass("off").removeClass("on");
        div.find('.switch@L_262_1@BACk').html("off");

    }
};
//Set up the varIoUs div
$('.switchOnOff .switchButton').each(function() {
    switcher(this);
});
//attach the handler
$('.switchOnOff .switchButton').click(function() {
    switcher(this);
});

在这里小提琴:http://jsfiddle.net/YpfmD/4/

@H_404_53@

大佬总结

以上是大佬教程为你收集整理的从jquery中的属性开始,在遍历dom的系列中更改单个开/关开关的状态全部内容,希望文章能够帮你解决从jquery中的属性开始,在遍历dom的系列中更改单个开/关开关的状态所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。