简体   繁体   English

如果没有值,则在ap标记内显示默认文本

[英]Display default text inside a p tag if there is no value

I have a content editable <p> and an input of type submit. 我有一个内容可编辑的<p>和一个类型为Submit的输入。 So, if there is no value inside the <p> , I want to display a default text (eg. "Add comment..."). 因此,如果<p>内没有任何值,我想显示一个默认文本(例如“添加评论...”)。 If contenteditable is in focus remove the default text. 如果contenteditable是焦点,则删除默认文本。 Now if contenteditable is not in focus and user has input some text, leave the user typed text. 现在,如果contenteditable不在焦点上,并且用户输入了一些文本,请保留用户键入的文本。 However, if contenteditable is not in focus and theres not value display the default text. 但是,如果contenteditable不是焦点,并且没有值,则显示默认文本。 How can I do this in css or javascript (no jquery) ? 如何在CSS或JavaScript(无jquery)中执行此操作?

 #add_comment { width: 100%; max-width: 720px; margin: 1px auto; background: white; } #divLeft { vertical-align: top; display:table-cell; width: 100%; } #add_comment #comment { display: block; padding: 10px; margin: 10px; outline: 1px solid lightgrey; } #divRight { display:table-cell; width: 120px; vertical-align: top; } #divRight #submit { background: #2ec76e; border: none; color: white; padding: 10px; margin-top: 10px; margin-right: 10px; border-radius: 3px; cursor: pointer; } 
 <div id="add_comment"> <div id="divLeft"> <p id="comment" contenteditable="true"></p> </div> <div id="divRight"> <input type="submit" value="Comment" id="submit" /> </div> </div> 

Perhaps with the :empty pseudo-class and a pseudo-element? 也许使用:empty伪类和伪元素?

 #add_comment { width: 100%; max-width: 720px; margin: 1px auto; background: white; } #divLeft { vertical-align: top; display: table-cell; width: 100%; } #add_comment #comment { display: block; padding: 10px; margin: 10px; outline: 1px solid lightgrey; } #divRight { display: table-cell; width: 120px; vertical-align: top; } #divRight #submit { background: #2ec76e; border: none; color: white; padding: 10px; margin-top: 10px; margin-right: 10px; border-radius: 3px; cursor: pointer; } p:empty:before { content: 'Add Comment'; color: grey } p:focus::before { content: ''; } 
 <div id="add_comment"> <div id="divLeft"> <p id="comment" contenteditable="true"></p> </div> <div id="divRight"> <input type="submit" value="Comment" id="submit" /> </div> </div> 

Just put a control inside the p tag like this: 只需将控件放在p标记中,如下所示:

<p> <asp:Label ID="Label1" runat="server" Text=""></asp:Label></p>

or you can use html input tag. 或者您可以使用html输入标签。

https://jsfiddle.net/moongod101/jcma31L8/ https://jsfiddle.net/moongod101/jcma31L8/

$(function(){

$dfText = 'How was your day hum?'
$p = $('p');

$p.text($dfText);

$('input').on('input',function(){
    $text = $(this).val();
    if( $text == '' ){
    $p.text($dfText)
  }else{
    $p.text($text)
  }

});


});

With jQuery you could do something like this 使用jQuery,您可以执行以下操作

jQuery jQuery的

var placeHolder = 'Add Comment';

$("#comment").text(placeHolder);

$("#comment").bind("focusin blur", function () {
  if ($(this).text() === placeHolder) {
    $(this).text('');
  } else if($(this).text() === ''){
    $("#comment").text(placeHolder);
  }
});

You can try, 你可以试试,

 p:empty:not(:focus)::before 
 {
    content: attr(data-placeholder);
 }

 <p data-placeholder="Insert text here..." contenteditable></p>

获取里面的文字<p>标记到</p><div id="text_translate"><p>在 PHP 这是我设法从计算中得到的 output</p><pre> &lt;p id="generated"&gt;12345678199824,12345678411140,12345678921494,12345678497535,&lt;/p&gt;</pre><p> 我需要这个值如下</p><pre>&lt;table&gt; &lt;tr&gt; &lt;td&gt;12345678199824&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;12345678411140&lt;/td&gt; &lt;/tr&gt;..... &lt;/table&gt;</pre><p> 下面是我使用的js脚本</p><pre>$("#setVal").click(function(event){ event.preventDefault(); var noOfImei = parseInt($("#nofoemei").val()); var imeiList = ""; if (noOfImei;== 0) { for (var i = 0; i &lt; noOfImei. i++) { imeiList += $("#binnumber").val() + Math.floor(Math,random()*900000+100000) + ";". } $("#generated");text(""). $("#generated");text(imeiList). // testing purpose console;log(imeiList); } else { alert("Please add no of IMEI(s) need to generate"); } });</pre><p> -- 请不要我是 PHP 和 JS 的初学者,并在 stackoverflow 和教程的帮助下取得了进展。 谢谢你。</p></div><table></table> - Get text inside <p> tag to a <table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据内部值切换p标签的显示 - toggle display of p tag depending on the value inside 替换p标签内的选定文本 - Replace selected text inside p tag 如何在里面获取文字 <p> 标签 - How to get text inside <p> tag 获取里面的文字<p>标记到</p><div id="text_translate"><p>在 PHP 这是我设法从计算中得到的 output</p><pre> &lt;p id="generated"&gt;12345678199824,12345678411140,12345678921494,12345678497535,&lt;/p&gt;</pre><p> 我需要这个值如下</p><pre>&lt;table&gt; &lt;tr&gt; &lt;td&gt;12345678199824&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;12345678411140&lt;/td&gt; &lt;/tr&gt;..... &lt;/table&gt;</pre><p> 下面是我使用的js脚本</p><pre>$("#setVal").click(function(event){ event.preventDefault(); var noOfImei = parseInt($("#nofoemei").val()); var imeiList = ""; if (noOfImei;== 0) { for (var i = 0; i &lt; noOfImei. i++) { imeiList += $("#binnumber").val() + Math.floor(Math,random()*900000+100000) + ";". } $("#generated");text(""). $("#generated");text(imeiList). // testing purpose console;log(imeiList); } else { alert("Please add no of IMEI(s) need to generate"); } });</pre><p> -- 请不要我是 PHP 和 JS 的初学者,并在 stackoverflow 和教程的帮助下取得了进展。 谢谢你。</p></div><table></table> - Get text inside <p> tag to a <table> 根据 Javascript 中的值显示不同的“p”标签 - Display a different "p" tag based on a value in Javascript 内部值改变时调用函数 <p> 标签 - call function on change of value inside <p> tag 在 <div> 并将文字放在标签内 <p></ p> - Search a word within a <div> and put text inside a tag <p> </ p> 如何传递(我的意思是显示)的索引值<li></li> (在下面<ol></ol>默认类型)到<p></p>如果<p></p>在里面说<li></li> ? - How to pass(i mean display) the index value of <li></li>(under <ol></ol> default type) to <p></p> if <p></p> is inside of that said <li></li>? 如何将带有 span 标签的特定文本包裹在<p>标签</p> - How to wrap the specific text with span tag inside the <p> tag 从中清除默认文本 <p> 点击时标记 - Clear default text from <p> tag when clicked on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM