【问题标题】:jQuery issue when append() text contains linksappend() 文本包含链接时的 jQuery 问题
【发布时间】:2011-11-24 20:34:28
【问题描述】:

我的应用(单页布局)从服务器接收数据,我想要一个漂亮的基于按钮的导航,例如:

<a data-role="button">Button X</a>
<a data-role="button">Button Y</a>

非常直接。当用户在程序中来回移动时,我想重用我的 div,所以我在使用 append() 插入新按钮之前调用$('#fancyDiv').empty() - 根据程序状态。

好的,但现在我们遇到了麻烦。只有在第一次通话按钮才能正确显示。当我返回时,选择另一个菜单项并刷新页面(调用empty()append())除了纯文本之外没有任何按钮。

最小化我的代码吧,好像 jQuery 不喜欢附加 HTML 锚标签。
您可以在下面找到一些要在 Web 浏览器中打开的 HTML 代码。附加第二个文本时,您可以看到链接没有着色,而是被忽略并显示为常规文本。

我还尝试了 html()text() 方法导致相同的结果,所以我很感激任何想法。

<html>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>

    <body>
        <a data-role="button" onclick="$('#text').empty();">Empty paragraph</a>
        <a data-role="button" onclick="$('#text').append('wawon Park is a park that is located in Daegu Dalseong-gun, South Korea. The people who mainly use the park are children. It is marked to park, <a>1981 March 10.</a>')">Attach new Text</a>

        <p id="text">Hod Stuart (1879-1907) was a <a>Canadian professional</a> ice hockey cover-point (now known as a defenceman) who played nine seasons for several teams in different leagues. He also played briefly for the Ottawa Rough Riders football team</p>

        <h3>Reference Text</h3>
        <p>wawon Park is a park that is located in Daegu Dalseong-gun, South Korea. The people who mainly use the park are children. It is marked to park, <a>1981 March 10.</a></p>
    </body>
</html>

* 编辑/更新 *

好的,Rory McCrossan 提出了一个非常好的建议,即手动将 ui-link 类添加到锚元素。这在技术上解决了我的问题,但更进一步,出现了一个新问题。我很确定是否为此打开一个新问题:

如前所述,我想使用按钮进行导航,仅向它们添加 ui-link 类是不够的。我用萤火虫检查了this fiddle,似乎缺少某种启动方法。 这是工作按钮的代码:

<a class="ui-btn ui-btn-up-c ui-btn-corner-all ui-shadow" data-role="button" data-theme="c">
<span class="ui-btn-inner ui-btn-corner-all" aria-hidden="true">
<span class="ui-btn-text">1981 March 10.</span>
</span>
</a>

我们在这里找到附加的按钮:

<a class="ui-link" data-role="button">1981 March 10.</a>

好的,又快又脏,我可以自己复制代码并设置所有跨度,但这听起来....错误,一旦 jQuery 的结构发生一些变化就会中断。

【问题讨论】:

标签: jquery hyperlink


【解决方案1】:

您的代码运行良好。 A 标签被附加,但它没有以任何方式设置样式。如果您将类 ui-link 添加到 A 标记,它看起来与其他标记相同。我假设这个类是由您包含的jquery-mobile 脚本在加载时添加的。

有关示例,请参阅 this fiddle

请注意,我还将每个按钮的处理代码移到了它自己的点击处理程序中,以便为您提供更简洁的代码。

【讨论】:

  • 谢谢。这解决了链接的问题。但是我尝试实现它,它似乎有点复杂,因为我正在使用按钮。我已经 link 你的小提琴分配了 data-role="button" 给链接。 -sight- 似乎没有成功。编辑:我试图复制你的想法,并为锚标签分配了更多的类,比如 ui-btn 和其他一些,但它并没有真正奏效。 (以及如何在 cmets 中获得新行?<.>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
相关资源
最近更新 更多