【问题标题】:Using JSF h:outputLink to produce a page anchor使用 JSF h:outputLink 生成页面锚点
【发布时间】:2012-12-25 22:37:48
【问题描述】:

简单的问题:

如何创建 HTML 锚点

<a id="organization" />

使用 JSF,例如

<h:outputLink ... />

还是其他 JSF 链接组件? 有可能吗?

【问题讨论】:

    标签: html jsf jsf-2 anchor


    【解决方案1】:
    <h:link value="test" outcome="icerik">
        <f:param name="id" value="#{icerik.id}" />
    </h:link>
    

    这个链接到icerik.xhtml?id=2,看起来像&lt;a href="icerik.xhtml.?id=2"&gt;test&lt;/a&gt;

    【讨论】:

      【解决方案2】:

      可以为此使用&lt;h:link&gt;。它的id 属性变为&lt;a id&gt;&lt;a name&gt;

      <h:link id="organization" value="Organization" fragment="organization" />
      

      它会生成以下 HTML:

      <a id="organization" name="organization" href="/currentcontext/currentpage.xhtml#organization">Organization</a>
      

      但在 JSF/HTML 中仅使用普通的 &lt;a&gt; 甚至 &lt;span&gt;&lt;div&gt; 作为跳转目标是完全合法的。

      <span id="organization">Organization</span>
      

      要创建跳转到该链接的链接,请使用 &lt;h:link fragment&gt; 而不使用 id

      <h:link value="Jump to organization" fragment="organization" />
      

      生成的 HTML 将如下所示:

      <a href="/currentcontext/currentpage.xhtml#organization">Jump to organization</a>
      

      【讨论】:

        【解决方案3】:

        为什么不使用

        <h:outputLink value="www.yourweb/somePage#anchor" />
        

        或者如果它只是在同一页面上

        <h:outputLink value="#anchor" />    
        

        【讨论】:

        • 因为这会产生指向锚点的链接,而不是锚点本身?这实际上是这个问题的重点...... ;-)
        • 我不这么认为,你为什么不能使用&lt;a id="organization" /&gt; 语法?这样做完全没问题...
        • 我可以使用它——当然,但这不是问题所在。即使已经存在解决方案,我也很想知道。
        • 上一个答案就是解决方案。这段代码不会生成#anchor,只是生成了没有锚点的普通链接。
        猜你喜欢
        • 2013-08-06
        • 1970-01-01
        • 2011-02-12
        • 1970-01-01
        • 1970-01-01
        • 2014-08-25
        • 1970-01-01
        • 1970-01-01
        • 2016-01-31
        相关资源
        最近更新 更多