简体   繁体   English

在 Label 的悬停上显示工具提示?

[英]Display tooltip on Label's hover?

I have below HTML code ?我有以下 HTML 代码?

<label for="male">Hello This Will Have Some Value</label>

But actually i dont have enough space to show this much long label.但实际上我没有足够的空间来显示这么长的标签。 So i thought of creating label as below..所以我想创建如下标签..

<label for="male">Hello...</label>

Then i create a hidden field which will hold entire label value然后我创建一个隐藏字段,它将保存整个标签值

<input type="hidden" name="Language" value="Hello This Will Have Some Value">

Now when user hovers mouse on Hello... can i show hidden field's value Hello This Will Have Some Value in tooltip using jquery?现在,当用户将鼠标悬停在Hello...上时,我可以使用 jquery 在工具提示中显示隐藏字段的值Hello This Will Have Some Value吗?

Thanks!谢谢!

You can use the "title attribute" for label tag.您可以将“标题属性”用于标签标签。

<label title="Hello This Will Have Some Value">Hello...</label>

If you need more control over the looks,如果您需要更多地控制外观,

1 . 1 . try http://getbootstrap.com/javascript/#tooltips as shown below.尝试http://getbootstrap.com/javascript/#tooltips ,如下所示。 But you will need to include bootstrap.但是您需要包含引导程序。

<button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="left" title="Hello This Will Have Some Value">Hello...</button>

2 . 2 . try https://jqueryui.com/tooltip/ .试试https://jqueryui.com/tooltip/ But you will need to include jQueryUI.但是您需要包含 jQueryUI。

<script type="text/javascript">
$(document).ready(function(){
$(this).tooltip();
});
</script>

You don't have to use hidden field.您不必使用隐藏字段。 Use "title" property.使用“标题”属性。 It will show browser default tooltip.它将显示浏览器默认工具提示。 You can then use jQuery plugin (like before mentioned bootstrap tooltip) to show custom formatted tooltip.然后,您可以使用 jQuery 插件(如前面提到的引导工具提示)来显示自定义格式的工具提示。

<label for="male" title="Hello This Will Have Some Value">Hello ...</label>

Hint: you can also use css to trim text, that does not fit into the box (text-overflow property).提示:您还可以使用 css 来修剪不适合框的文本(text-overflow 属性)。 See http://jsfiddle.net/8eeHs/http://jsfiddle.net/8eeHs/

Just set a title on the label:只需在标签上设置一个标题:

<label for="male" title="Hello This Will Have Some Value">Hello...</label>

Using jQuery:使用jQuery:

<label for="male" data-title="Language" />
<input type="hidden" name="Language" value="Hello This Will Have Some Value">

$("label").prop("title", function() {
    return $("input[name='" + $(this).data("title") + "']").text();
});

If you can use CSS3, you can use the text-overflow: ellipsis;如果可以使用 CSS3,则可以使用text-overflow: ellipsis; to handle the ellipsis for you so all you need to do is copy the text from the label into the title attribute using jQuery:为您处理省略号,因此您需要做的就是使用 jQuery 将标签中的文本复制到 title 属性中:

HTML: HTML:

<label for="male">Hello This Will Have Some Value</label>

CSS: CSS:

label {
    display: inline-block;
    width: 50px;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}

jQuery: jQuery:

$("label").prop("title", function() {
   return $(this).text(); 
});

Example: http://jsfiddle.net/Xm8Xe/示例: http : //jsfiddle.net/Xm8Xe/

Finally, if you need robust and cross-browser support, you could use the DotDotDot jQuery plugin .最后,如果您需要强大的跨浏览器支持,您可以使用DotDotDot jQuery 插件

Are you find with using standard tooltip?您是否发现使用标准工具提示? You could use title attribute like您可以使用标题属性,如

<label for="male" title="Hello This Will Have Some Value">Hello...</label>

You could add the title attribute of same value to the element that label is for as well.您也可以将相同值的 title 属性添加到 label 的元素。

If you also have jQuery UI, you can add this:如果你也有 jQuery UI,你可以添加这个:

$(document).ready(function () {
  $(document).tooltip();
});

You then need a title instead of a hidden input.然后你需要一个标题而不是一个隐藏的输入。 RGraham already posted an answer doing this for you :P RGraham 已经为您发布了一个答案:P

You can use the css-property content and attr to display the content of an attribute in an :after pseudo element.您可以使用 css-property contentattr:after伪元素中显示属性的内容。 You could either use the default title attribute (which is a semantic solution), or create a custom attribute, eg data-title .您可以使用默认的 title 属性(这是一种语义解决方案),也可以创建一个自定义属性,例如data-title

HTML: HTML:

<label for="male" data-title="Please, refer to Wikipedia!">Male</label>

CSS: CSS:

label[data-title]{
  position: relative;
  &:hover:after{
    font-size: 1rem;
    font-weight: normal;
    display: block;
    position: absolute;
    left: -8em;
    bottom: 2em;
    content:  attr(data-title);
    background-color: white;
    width: 20em;
    text-aling: center;
  }
}

You could use the title attribute in html :)您可以在 html 中使用title属性:)

<label title="This is the full title of the label">This is the...</label>

When you keep the mouse over for a brief moment, it should pop up with a box, containing the full title.当您将鼠标悬停片刻时,它应该会弹出一个包含完整标题的框。

If you want more control, I suggest you look into the Tipsy Plugin for jQuery - It can be found at http://onehackoranother.com/projects/jquery/tipsy/ and is fairly simple to get started with.如果您想要更多控制,我建议您查看用于 jQuery 的 Tipsy 插件 - 它可以在http://onehackoranother.com/projects/jquery/tipsy/上找到,并且非常容易上手。

Tooltips in bootstrap are good, but there is also a very good tooltip function in jQuery UI, which you can read about here . bootstrap 中的工具提示很好,但 jQuery UI 中也有一个非常好的工具提示功能,你可以在这里阅读。

Example:例子:

<label for="male" id="foo" title="Hello This Will Have Some Value">Hello...</label>

Then, assuming you have already referenced jQuery and jQueryUI libraries in your head, add a script element like this:然后,假设您已经在脑海中引用了 jQuery 和 jQueryUI 库,请添加如下脚本元素:

<script type="text/javascript">
$(document).ready(function(){
    $(this).tooltip();
});
</script>

This will display the contents of the title attribute as a tooltip when you rollover any element with a title attribute.当您滚动具有 title 属性的任何元素时,这将显示 title 属性的内容作为工具提示。

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

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