如果可编辑div没有设置占位符的文本 [英] If editable div have no text than set placeholder

查看:559
本文介绍了如果可编辑div没有设置占位符的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



其实我正在使用 contentEditable ='true'; 这个元素在div上。我用jQuery处理占位符到这个函数。 $($'$'$'$'$ $ $ $ $ $ $ $ $'$ $ $ $'$ $ $ $ $''$'$'$'$' $(bb $ b $(this).empty();
var range,sel;
if((sel = document.selection)&&& document.body.createTextRange){
range = document.body.createTextRange();
range.moveToElementText(this);
range.select();}});如果(this.hasChildNodes()&& amp;& document.createRange&&& window.getSelection($) ){

$(this).empty();
var range,sel;
range = document.createRange();
range.selectNodeContents(this) ;
sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}});

`

解决方案你可以这样做:

 < div contenteditable =trueid =editDiv> Placeholer< ; / DIV> 



jQuery

  var placeholder =占位符; //将其改为占位符文本
$(#editDiv)。focus(function(){
if($(this).text()== placeholder){
$ (this).text();
}
})。focusout(function(){
if(!$(this).text()。length){
$(this).text(占位符);
}
});


I am try to make editable div with placeholder.But i want when no text in this div than automatically placeholder set.

Actually i am using contentEditable='true'; this element On div.And i am handle placeholder with jQuery to this function. `

     $('div').on('activate',
        function() {
       $(this).empty();
       var range, sel;
     if ( (sel = document.selection) && document.body.createTextRange){
    range = document.body.createTextRange();
    range.moveToElementText(this);
    range.select();} });    


    $('div').focus(function() {
    if (this.hasChildNodes() && document.createRange && window.getSelection) {

   $(this).empty();
    var range, sel;
    range = document.createRange();
    range.selectNodeContents(this);
    sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
}});

`

解决方案

You can do this:

<div contenteditable="true" id="editDiv">Placeholer</div>

jQuery

var placeholder = "Placeholder"; //Change this to your placeholder text
$("#editDiv").focus(function() {
    if ($(this).text() == placeholder) {
        $(this).text("");
    }
}).focusout(function() {
    if (!$(this).text().length) {
        $(this).text(placeholder);
    }
});

这篇关于如果可编辑div没有设置占位符的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆