2012-06-04 3 views
5

.hasClass()라는 플러그인이 있다는 것을 알고 있습니다.jQuery check 요소 ID

내가 가지고있는 내가 page_background가 클릭하면 확인 header_background하지 않을 방법

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

을 다음?

이것은 내가 지금 가지고있는 것입니다. 작동하지 않습니다. this는 jQuery를 객체가 아닌 DOM 노드 인 경우 당신이 할 수있는,

var id = this.id; 

:

$('#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); 
    }); 
+0

id 사용을 얻을 수 있습니다 id를 찾고'background_color' howe 당신은 그 id를 가진 요소에 click 이벤트를 바인딩하지 않습니다. (당신이 방금 다시 되돌려 준 편집을 바탕으로) –

답변

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

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

Working fiddle

이 내부의 ColorPicker를 onSubmit 기능이 el 등의 요소를 얻을

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

를 사용하는 것처럼, 그래서 당신이

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

가 작동하지 않습니다. 무엇이 문제입니까? – Grigor

+0

바이올린을 확인하고 텍스트를 클릭하십시오. –

+0

this.attr이 함수가 아닙니다. – Grigor

4

당신이 this에 저장된 클릭 요소에 대한 참조를 가정하면, 당신은 단순히 기본 DOM 속성을 사용할 수 있습니다 this.attr("id")을 사용하십시오.

또한 hasClass은 일반적인 jQuery 메서드이며 플러그인이 아닙니다. 이는 표준 jQuery 라이브러리의 일부입니다.

+0

... 또는'prop ("id")'(새 버전에서). – VisioN