这个小提琴:http://jsfiddle.net/2mw4c/12/
我希望能够清除<p>
标记中的文字,然后开始输入。我遇到的问题是,当您点击时,<p>
标记会被清除,我无法输入。
JavaScript的:
$("p").on("mousedown", function(e){
if($(this).text() === "Click"){
$(this).text("").focus();
}
});
HTML:
<div class="content" contenteditable="true"><p>Click</p></div>
我需要做什么才能开始在标签中写作?
答案 0 :(得分:5)
我不确切知道您要实现的目标,但是当我将contenteditable
移动到p
元素时,它对我有用。
我从您的评论中得到的印象是,您希望将当前结构保留在标记中。由于div
为空,p
似乎正在崩溃。我已经更新了我的小提琴链接。
答案 1 :(得分:1)
$("p").on("mousedown", function (e) {
var that = $(this);
var txt = that.text();
if (txt === "Click") {
height = that.css('height');
that.css('height', height).text("").focus();
} else if ((txt).length) {
that.css('height', 'inherit');
} else if (txt.length === 0) {
that.css('height', height);
}
});
答案 2 :(得分:1)
您可以设置最小高度和宽度。您需要指定大小,否则p-tag内将没有可点击区域,因为in是内联元素。
http://jsfiddle.net/MrPolywhirl/WZg6H/
div.content>p {
min-width: 1em;
min-height: 1em;
background: #EEF;
}
答案 3 :(得分:0)