【问题标题】:Nesting <a> element inside Html.LabelFor在 Html.LabelFor 中嵌套 <a> 元素
【发布时间】:2014-12-23 00:54:42
【问题描述】:
<div class="form-group col-sm-5">
    @Html.LabelFor(m => m.foo)
    <a data-toggle="popover" data-content="I'm a popover!" href="#"> <span class="glyphicon glyphicon-info-sign"></span></a>
</div>

&lt;a&gt; 元素(即字形弹出框)出现在 label 元素的下方,但我希望将其放在 右侧而是标签。有没有办法通过将&lt;a&gt; 嵌套在@Html.LabelFor 中来实现这一点?有这样的东西会很酷:

<label for="foo"> I'm the label!
    <a data-toggle="popover" data-content="I'm a popover!" href="#"> <span class="glyphicon glyphicon-info-sign"></span></a>
</label>

如果不是,我是否必须使用定位来代替?


编辑:我最终接受了 Ramo 的建议,并采用了一些简单的方法。

<div class="form-group col-sm-5">
    <label>
       <span> I'm a **hard-coded** LabelFor DataAnnotation! </span>
       <a data-toggle="popover" data-content="I'm a popover!" href="#">
           <span class="glyphicon glyphicon-info-sign"></span>
       </a>
    </label>
</div>

如果有一种 MVC 方法可以让 Glyphicons 与 @Html.LabelFor 保持一致,我仍然感兴趣,但我的问题在这一点上或多或少是学术性的。


编辑#2:我发现了一个更简单的解决方案,事后看来这是显而易见的。这种方法使用定位(即display: inline)来确保伴随的&lt;a&gt;&lt;span&gt; 元素显示在同一行。

<div class="col-sm-5 form-group">
    @Html.LabelFor(m => m.foo, new { style = "display: inline;" })
    <a data-toggle="popover" data-content="I'm a popover!" href="#"> <span class="glyphicon glyphicon-info-sign"></span></a>
</div>

【问题讨论】:

    标签: html css asp.net-mvc twitter-bootstrap


    【解决方案1】:

    你可以用一张桌子做这样的事情

    <table>
        <tr>
            <td>@Html.LabelFor(m => m.foo)</td>
            <td><a data-toggle="popover" data-content="I'm a popover!" href="#"> <span class="glyphicon glyphicon-info-sign"></span></a></td>
        </tr>
    </table>
    

    编辑:

    一个快速的谷歌搜索让我看到了这个结果,它使用 CSS 来实现你的目标Label and text box on the same line using css

    编辑 2:

    既然您厌倦了使用表格,您也许可以做这样的事情。我还没有测试过代码,但我相信它应该会给你你想要的行为。

    HTML:

    <div>
        <div class="Label">@Html.LabelFor(m => m.foo)</div>
             <a data-toggle="popover" data-content="I'm a popover!" href="#"> <span class="glyphicon glyphicon-info-sign"></span></a>
    </div>
    

    CSS:

    .Label{
            display: inline-block;
        }
    

    【讨论】:

    • 这确实产生了我正在寻找的所需类型的布局,尽管我对使用表格来实现这一点有点厌倦。我希望可能有一种更清洁的方法来做到这一点 - 也许是 MVC 中内置的东西。
    • @alex 我提出了一个新的解决方案,它不使用表格而只使用 CSS 来进行定位。我还没有测试过代码,但我认为这会起作用。试一试
    • 第二个提议的解决方案不起作用,并产生与第一个不同的行为。 &lt;a&gt; 元素被推到 '
    • @alex 真的我刚试过。在您的 CSS 中,您是否放置了 .label 而不是 label,因为我在我发布的代码中忘记了 label 前面的句点。我刚刚对其进行了测试,它对我有用
    • @alex 现在我再看一遍标签应该在 css 中有一个大写 L
    【解决方案2】:

    我建议你继续定位。不要在这里采取不寻常的选择,将所有内容都用于应该使用的内容,否则您将来会注意到代码的异常行为。

    【讨论】:

    • 这更适合作为评论而不是答案。除了告诉该人再去谷歌搜索之外,您几乎没有提供任何有用的信息
    • Ramo 的建议最有帮助,因为在 MVC 中似乎没有任何方法可以得到我正在寻找的特定行为。
    【解决方案3】:

    试试这个:

    @{
    var foo = Html.LabelFor(m => m.foo)
    }
    
    <a>@foo</a>
    

    【讨论】:

    • 我认为这会使整个标签在悬停时切换一个弹出框,而不仅仅是字形图标。
    猜你喜欢
    • 1970-01-01
    • 2013-02-02
    • 2023-03-23
    • 2020-06-11
    • 2011-09-17
    • 2012-02-18
    • 2014-05-22
    • 2016-11-24
    相关资源
    最近更新 更多