【问题标题】:Struts2 : How to show a tooltip for each dropdown option in a select tag?Struts2:如何在选择标签中显示每个下拉选项的工具提示?
【发布时间】:2011-08-04 23:14:28
【问题描述】:

我有这个 struts2 选择标签,其中选项是 Item 对象的列表。 假设 Item java bean 类具有以下 3 个属性:itemId, itemLabel, itemDescription

我的选择标签看起来像这样:

<s:select headerKey="" 
  headerValue="Select Item"
  list="myModel.itemsList"
  listKey="itemId"
  listValue="itemLabel"
  name="selectedItem" />   

每当用户悬停在该选项上时,我想在下拉菜单中为每个选项显示一个工具提示。填充该工具提示的文本存储在我的 java bean 类的 itemDescription 属性中

我知道你可以给选择标签一个顶部提示,但这不是我需要的,因为每个选项/项目都有不同的描述。

目前我有一个自定义的 javascript 工具提示,如果可能的话,我想使用这个功能。如果项目将列在表格中,这就是我将如何使用该函数:

<td>
    <span class="tooltip" onmouseout="tooltip.hide();"
      onmouseover="tooltip.show('<s:property value="item.description" />');">
        <s:property value="item.label" />
    </span>
</td>

有谁知道每次用户将鼠标悬停在某个选项上时,将description 显示为工具提示的最佳解决方案是什么?

【问题讨论】:

    标签: select struts2 tooltip


    【解决方案1】:

    有很多方法可以做你想做的事。对我来说最快的就是写 html:

        <select name="selectedItem">
            <s:iterator value="collectionOfSomething">
                <option value="<s:property value="valueToReturn"/>" title="<s:property value="toolTip"/>">
                    <s:property value="itemInListToShow"/>
                </option>
            </s:iterator>
        </select>
    

    上面使用了 html5 的 title 属性,这样做的好处是你可以在大多数现代浏览器中获得一个工具提示,而无需任何 javascript。

    将“valueToReturn”、“toolTip”和“itemInListToShow”替换为“collectionOfSomething”中的值的适当 ognl 表达式

    解决这个问题的另一种方法是使用 jQuery(任何 JS 库都可以),并且会像这样。

    1) 在页面上放置一个带有工具提示的 html 列表,但使用 CSS 设置样式,因此不可见。

    2) 使用 jQuery(或偏好的 JS 库)将鼠标悬停事件绑定到下拉列表中的每个项目,这将显示隐藏列表中相同索引的工具提示。

    【讨论】:

    • 事后查看 hoss 的解决方案,如果您使用 JS 解决方案,我会修改 #2 以让 Tipsy 处理显示,因为它太酷了。虽然速度不快,但我仍然会使用直接的 html 解决方案。
    • 嘿,非常感谢四元数。我选择了第一个使用纯 html 和填充标题的实现。
    【解决方案2】:

    我建议你也使用tipsy,http://onehackoranother.com/projects/jquery/tipsy/是我找到的最好的解决方案,我也在使用它。

    声明 .js 和 .css 文件的路径

    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/tipsy.css" type="text/css" />
        <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/tipsy-docs.css" type="text/css" />
    <script type="text/javascript" src="${pageContext.request.contextPath}/resources/scripts/jquery.tipsy.js"></script>
    
     <script type='text/javascript'>
       $(function() {
        $('.example-1').tipsy();
        $('.north').tipsy({gravity: 'n', html: true });
        $('.south').tipsy({gravity: 's', html: true });
        $('.east').tipsy({gravity: 'e', html: true });
        $('.west').tipsy({gravity: 'w', html: true });
        $('.auto-gravity').tipsy({gravity: $.fn.tipsy.autoNS, html: true});
        $('.example-fade').tipsy({fade: true, html: true});
        $('.example-custom-attribute').tipsy({title: 'id', html: true});
        $('.example-callback').tipsy({title: function() { return this.getAttribute('original-title').toUpperCase(); } });
        $('.example-fallback').tipsy({fallback: "Where's my tooltip yo'?" });
        $('.example-html').tipsy({html: true });
    });
    </script>
    

    最后一件事,把你的选择放在这个范围之间:

     <span class="south" style="cursor: pointer;" title=Your title"></span>
    

    这个 span 中的 class 属性是用于工具提示对齐的,很奇怪,但它的工作原理是相反的。没有这么大的问题,如果你需要把它对准北,就写南。

    【讨论】:

    • 您好,老板,我不确定您是否理解我的问题。我不想要下拉框(选择标签)的工具提示,而是其中的每个选项。在您的示例中,我看不到如何使下拉菜单中的每个选项从item.description 属性中获取工具提示?
    【解决方案3】:

    使用jQuery可以轻松解决

    onmouseoverbutton事件在ui中调用以下函数

    函数 addTitleAttributes(值){

    $("#"+value+" option").each(function()
    {
            $(this).attr('title',$(this).text());
    });
    

    }

    传递的值可以是选择按钮的id

    【讨论】:

    • 这个超级简单,喜欢。
    • 我收到错误:- 语法错误,无法识别的表达式:#[object] 选项对此有何解决方案??
    猜你喜欢
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 2019-05-28
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多