Jquery如果contenteditable = true事件

afr*_*360 3 jquery contenteditable

我试图检测我点击的元素是否是contentEditable.我试过这个:

$( "#thediv" ).hide();    
$(this).click(function() {
    if ($(this).is("[contenteditable='true']")) {
    return true;
    $( "#thediv" ).show();
    alert('hi');
} else if ($(this).is("[contenteditable='false']")) {
    return false;
    $( "#thediv" ).hide();
    alert('hi');            
}
}); 
Run Code Online (Sandbox Code Playgroud)

$( "#thediv" ).hide();  

$(this).click(function() {
    if ($(this).attr("contentEditable") == true) {  
        $( "#thediv" ).show();
        alert('yes');
    } else {
        $( "#thediv" ).hide();  
        alert('no');          
    }    
}); 
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它发挥作用?

这是一个小提琴

Tim*_*own 9

contenteditable属性将告诉您元素是否具有为contenteditable设置的显式值.但是,可编辑性是继承的,因此<span>下面的HTML中的元素是可编辑的,但没有contenteditable属性值:

<div contenteditable="true"><span>Some editable text</span></div>
Run Code Online (Sandbox Code Playgroud)

你需要的是isContentEditable属性,它完全符合你的要求:

$(this).click(function() {
    if (this.isContentEditable) {
        // Do stuff
    }
});
Run Code Online (Sandbox Code Playgroud)