我试图检测我单击的元素是否是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');
}
}); 和
$( "#thediv" ).hide();
$(this).click(function() {
if ($(this).attr("contentEditable") == true) {
$( "#thediv" ).show();
alert('yes');
} else {
$( "#thediv" ).hide();
alert('no');
}
}); 我怎么才能让这个起作用?
这是一个小提琴
发布于 2014-05-29 22:04:15
这是没有定义的。Ya go http://jsfiddle.net/dqEE2/2/
$( "#hello" ).hide();
$(function() {
$("div").click(function() {
if ($(this).attr("contentEditable") == "true") {
$( "#hello" ).show();
alert("yes");
} else {
alert("no");
$( "#hello" ).hide();
}
});发布于 2014-05-30 08:29:29
contenteditable属性将告诉您元素是否为内容可编辑设置了显式值。但是,可编辑性是继承的,因此下面的<span>这样的元素是可编辑的,但是没有contenteditable属性值:
<div contenteditable="true"><span>Some editable text</span></div>相反,您需要的是属性,它完全满足您的需要:
$(this).click(function() {
if (this.isContentEditable) {
// Do stuff
}
});发布于 2014-05-29 21:20:39
您试过使用jQuery的attr?http://api.jquery.com/attr/吗?
if ($(this).attr("contentEditable") == "true") {编辑:下面的工作非常好:
if ($(this).is("[contentEditable='true']")) {AFro360的问题是不一样的。
https://stackoverflow.com/questions/23943085
复制相似问题