Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 将类从单击的元素转移到另一个元素_Jquery_Class_Html Select - Fatal编程技术网

Jquery 将类从单击的元素转移到另一个元素

Jquery 将类从单击的元素转移到另一个元素,jquery,class,html-select,Jquery,Class,Html Select,我正在制作一个语言选择器——想象一个带有选项的select标记。 每个选项标记都有一个带有唯一类的范围 我想做的事情是,单击时将该类转移到select标记 我没有任何代码,那么jQuery是如何做到这一点的呢 $('select').change(function(){ var t = $(this), o = t.find('option:selected span'); t.addClass(o.attr('class')); o.removeClass(); }); 你

我正在制作一个语言选择器——想象一个带有选项的select标记。 每个选项标记都有一个带有唯一类的范围

我想做的事情是,单击时将该类转移到select标记

我没有任何代码,那么jQuery是如何做到这一点的呢

$('select').change(function(){
   var t = $(this), o = t.find('option:selected span');
   t.addClass(o.attr('class'));
   o.removeClass();
});

你能提供该部分的HTML代码吗? 你是说
里面有

那不太好,明白吗

但是现在我假设您想要将
的类复制到


默认情况下,onload您的span标记将消失。说到这里,我给大家举了一个简单的例子

。我建议你通过一些css魔法继续这个项目。下面是一些快速的细节

<select>
    <option selected>select</option>
    <option class="a">1</option>
    <option class="b">2</option>
    <option class="c">3</option>
    <option class="d">4</option>
    <option class="e">5</option>
</select>

<script>
$(function(){
    $('select').change(function(){
        if($(this).val() == 'select'){ 
            $(this).removeAttr('class')
        }else{
            $(this).find('option').each(function(){
                if($(this).text() == $(this).parent().val()){
                    $(this).parent().attr('class',$(this).attr('class'));
                }
            })
            alert($(this).attr('class')); // Select new class Value
        }
    })
})
</script>

选择
1.
2.
3.
4.
5.
$(函数(){
$('select').change(函数(){
如果($(this).val()=='select'){
$(this.removeAttr('class'))
}否则{
$(this).find('option').each(function(){
if($(this.text()==$(this.parent().val()){
$(this.parent().attr('class',$(this.attr('class'));
}
})
警报($(this.attr('class'));//选择新的类值
}
})
})
<select>
    <option selected>select</option>
    <option class="a">1</option>
    <option class="b">2</option>
    <option class="c">3</option>
    <option class="d">4</option>
    <option class="e">5</option>
</select>

<script>
$(function(){
    $('select').change(function(){
        if($(this).val() == 'select'){ 
            $(this).removeAttr('class')
        }else{
            $(this).find('option').each(function(){
                if($(this).text() == $(this).parent().val()){
                    $(this).parent().attr('class',$(this).attr('class'));
                }
            })
            alert($(this).attr('class')); // Select new class Value
        }
    })
})
</script>