jQuery 检查元素 ID [英] jQuery check element ID

查看:28
本文介绍了jQuery 检查元素 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一个插件叫做 .hasClass();

I know there is plugin called .hasClass();

我有以下内容

$('#page_background, #header_background').ColorPicker({...

如何检查是否点击了 page_background 而不是 header_background?

how would I check if page_background is clicked and not header_background?

这就是我现在所拥有的,它行不通.

This is what I have now, it won't work.

$('#page_background, #header_background').ColorPicker({
        onSubmit: function(hsb, hex, rgb, el) {
            $(el).val(hex);
            $(el).ColorPickerHide();

            var id = this.id;

            if(id == 'page_background')
                $('body').css("background-color","#"+hex);
        },
        onBeforeShow: function () {
            $(this).ColorPickerSetColor(this.value);
        }
    })
    .bind('keyup', function(){
        $(this).ColorPickerSetColor(this.value);
    });

推荐答案

$(function(){
   $('#page_background, #header_background').click(function(){

   var id = this.id;
   if(id == 'page_background')
     // page background
   else
     //header 
 });
});

工作小提琴

当你在 colorpicker onSubmit 函数中使用它

As you are using this inside colorpicker onSubmit function

onSubmit: function(hsb, hex, rgb, el) {

在那里您将元素作为 el 获取,因此要获取 id 使用

where you get the element as el, so to get the id use

 var id = $(el).attr('id');
 // or
 var id = $(el).prop('id'); // new way
 //or simply
 var id = el.id; // this should work too

这篇关于jQuery 检查元素 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆