首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery if contenteditable=true事件

Jquery if contenteditable=true事件
EN

Stack Overflow用户
提问于 2014-05-29 21:11:46
回答 4查看 8.5K关注 0票数 3

我试图检测我单击的元素是否是contentEditable。我试过这样做:

代码语言:javascript
复制
$( "#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');            
}
}); 

代码语言:javascript
复制
$( "#thediv" ).hide();  

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

我怎么才能让这个起作用?

这是一个小提琴

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-05-29 22:04:15

这是没有定义的。Ya go http://jsfiddle.net/dqEE2/2/

代码语言:javascript
复制
$( "#hello" ).hide();  
$(function() {
    $("div").click(function() {
        if ($(this).attr("contentEditable") == "true") {
            $( "#hello" ).show();
            alert("yes");
        } else {
            alert("no");
          $( "#hello" ).hide(); 
        }
    });
票数 3
EN

Stack Overflow用户

发布于 2014-05-30 08:29:29

contenteditable属性将告诉您元素是否为内容可编辑设置了显式值。但是,可编辑性是继承的,因此下面的<span>这样的元素是可编辑的,但是没有contenteditable属性值:

代码语言:javascript
复制
<div contenteditable="true"><span>Some editable text</span></div>

相反,您需要的是属性,它完全满足您的需要:

代码语言:javascript
复制
$(this).click(function() {
    if (this.isContentEditable) {
        // Do stuff
    }
});
票数 9
EN

Stack Overflow用户

发布于 2014-05-29 21:20:39

您试过使用jQuery的attr?http://api.jquery.com/attr/吗?

代码语言:javascript
复制
if ($(this).attr("contentEditable") == "true") {

编辑:下面的工作非常好:

代码语言:javascript
复制
if ($(this).is("[contentEditable='true']")) {

AFro360的问题是不一样的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23943085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档