【问题标题】:Using jQuery to generate a href inside an anchor tag with content from xml使用 jQuery 在带有 xml 内容的锚标记内生成 href
【发布时间】:2015-10-23 21:56:03
【问题描述】:

所以我的代码有问题,经过数小时的谷歌搜索后,我似乎找不到解决方案..

我的代码如下所示:

$(function() {
           $.ajax({
               method: "GET",
               url: "xml/rss.xml",
               dataType: "xml",
               success: function(result){
                   $(result).find("rss channel").each(function () {
                      var channel = $(this).find("title").text();

                       $(".rss ul").append(
                           $("<li />").append(
                                $("<a href='#'>", {
                                    text: channel
                                }, "</a>")
                           )
                       );
                   });
               },
               error: function() {
                   alert("fail!");
               }
           }); 
        });

现在问题来了,当我像这样运行它时,标签会接收到 href 属性,但 xml 文本不会出现在列表元素中。

如果我像这样删除 href="#":

$(".rss ul").append(
    $("<li />").append(
        $("<a>", {
            text: channel
        }, "</a>")
    )
);

xml 文件中的文本显示在我的列表中,但锚点不可点击。我是不是用错了代码?

【问题讨论】:

    标签: javascript jquery html ajax xml


    【解决方案1】:

    a 标签的声明不正确。试试这个 -

    $("<a>", {
      text: channel,
      href: '#'
    })
    

    【讨论】:

      猜你喜欢
      • 2011-03-29
      • 2019-12-05
      • 2018-01-15
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多