简体   繁体   English

如何使用jquery在title属性中添加换行符

[英]How to add line break in title attribute using jquery

Please find my code below which adds a tooltip on mouseover event to a field in my survey engine.请在下面找到我的代码,该代码将鼠标悬停事件的工具提示添加到我的调查引擎中的字段中。 What I want to achieve is add line breaks to the tooltip.我想要实现的是在工具提示中添加换行符。 Any help is greatly appreciated.非常感谢任何帮助。

var $j = jQuery.noConflict();

$j('#choice31QID405').mouseover(function() { 
$j(this).attr('title','My name is Glenn. <Add a line break>. I am a good boy'. <Add a line break>. I live in New Delhi); 
})

$j('#choice31QID405').mouseout(function() { 
$j(this).removeAttr('title'); 
})

Use entity code &#010;使用实体代码&#010; for line break.换行。 Your code will look something like this:您的代码将如下所示:

$j(this).attr('title','My name is Glenn.&#010;I am a good boy'.&#010;I live in New Delhi);

Refer this FIDDLE参考这个FIDDLE

On modern browsers, you can just use a line break:在现代浏览器上,你可以只使用换行符:

 $("#target").attr("title", "Hello\\nWorld");
 <p title="Hello World"> This one is hardcoded in the HTML. </p> <p id="target"> This one is added later </p> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

That works fine on current (as of this writing) Chrome and Firefox, as well as IE11.这适用于当前(在撰写本文时)Chrome 和 Firefox 以及 IE11。

html entity will work as mentioned above html实体将如上所述工作

Here is the full list of htmlentities that you can map in your code 以下是您可以在代码中映射的完整列表

https://dev.w3.org/html5/html-author/charref https://dev.w3.org/html5/html-author/charref

use <Hr> tag in HTML to set line在 HTML 中使用<Hr>标签来设置line

here is working example,这是工作示例,

 var $j = jQuery.noConflict(); $j('#choice31QID405').mouseover(function() { $j(this).attr('title','My name is Glenn. <hr />. I am a good boy <hr /> I live in New Delhi'); $j('#test').html($j(this).attr('title')); }); $j('#choice31QID405').mouseout(function() { $j(this).removeAttr('title'); $j('#test').html(""); });
 <script src="https://code.jquery.com/jquery-2.1.4.js"></script> <title>JS Bin</title> <body> <div id="choice31QID405">Mouse over here</div> <div id="test">Tooltip will show up here..</div> </body>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM