将链接添加到 xml 标记并使用 jquery 解析它

发布于 2024-10-24 18:35:56 字数 908 浏览 1 评论 0原文

我可以用 jquery 解析 xml。现在我希望解析的文本是超链接而不是简单的文本。下面是我写的。

 $(xml).find("customers")
     .each(function(){
          $("#guys").append("<div class="mybox"><a href="+$(this).find('customer_link').text()+"target="_blank">"+$(this).find('customer_company').text()"</a></div>");                                                                                      

});

这是 XML

    <customer_company><![CDATA[Google<br>]]></customer_company>
    <customer_link>http://www.google.com</customer_link>

</myguys>

<myguys>

    <customer_company><![CDATA[EMC<br>]]></customer_company>
    <customer_link>http://www.emc.com</customer_link>

</myguys>
</info>

我知道逻辑上没有任何问题,我认为这只是 a 标签的语法错误。有人可以告诉我我在哪里犯了错误并指出我一个很好的教程吗

I can parse the xml with jquery. Now I want the parsed text to be a hyperlink instead of simple text. Below is what I have written.

 $(xml).find("customers")
     .each(function(){
          $("#guys").append("<div class="mybox"><a href="+$(this).find('customer_link').text()+"target="_blank">"+$(this).find('customer_company').text()"</a></div>");                                                                                      

});

And here is the XML

    <customer_company><![CDATA[Google<br>]]></customer_company>
    <customer_link>http://www.google.com</customer_link>

</myguys>

<myguys>

    <customer_company><![CDATA[EMC<br>]]></customer_company>
    <customer_link>http://www.emc.com</customer_link>

</myguys>
</info>

I know there is nothing wrong in logic, I think this is just a syntax error with the a tag. Can somebody tell me where I am doing a mistake here and point me to a nice tutorial

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉薄对峙 2024-10-31 18:35:56

您正在混合引用样式。单引号 (') 字符串可以包含未转义的双引号 ("),反之亦然。在双引号字符串中包含未转义的双引号会破坏您的代码。

$("#guys").append("<div class="mybox"><a href="+$(this).find('customer_link')
ss^   se^       ss^         se^   ss^       se^            ss^           se^   

.text()+"target="_blank">"+$(this).find('customer_company').text()"</a></div>"); 
      ss^     se^    ss^e^            ss^              se^      ss^        se^ 

ss == string start,
se == string end 

您可以看看它如何创建随机未定义的引用,例如 mybox,但不会构建您想要的字符串。
试试这个:

$("#guys").append("<div class='mybox'><a href=" + 
    $(this).find("customer_link").text() +
    "' target='_blank'>" + 
    $(this).find("customer_company").text() + 
    "</a></div>");  

或者,您可以使用 jQuery 构建所有元素,并完全避免在字符串中嵌入带引号的属性:

 $("#guys").append($("<div />")
                      .addClass("mybox")
                      .append($('<a/>', {  
                          href: $(this).find("customer_link").text(),  
                          target: "_blank",  
                          text: $(this).find("customer_company").text()  
                      })));

xml 中的 CDATA 会导致 jQuery 问题,因为它正在解析 xml就像它是 html 一样,而不是根据 xml 规范。如果您无法删除 CDATA,您可能最好使用 jParse 之类的插件来解析 xml
来自 customer_company 元素。

You're mixing quote styles. Single quoted (') strings can contain unescaped double quotes (") and vice-versa. Having unescaped double quotes in your double-quoted string is breaking your code.

$("#guys").append("<div class="mybox"><a href="+$(this).find('customer_link')
ss^   se^       ss^         se^   ss^       se^            ss^           se^   

.text()+"target="_blank">"+$(this).find('customer_company').text()"</a></div>"); 
      ss^     se^    ss^e^            ss^              se^      ss^        se^ 

ss == string start,
se == string end 

You can see how this creates random undefined references like mybox, but doesn't build the string you intended.
Try this:

$("#guys").append("<div class='mybox'><a href=" + 
    $(this).find("customer_link").text() +
    "' target='_blank'>" + 
    $(this).find("customer_company").text() + 
    "</a></div>");  

Alternatively, you can use jQuery to build all the elements and completely avoid having attributes with quotes embedded in your string:

 $("#guys").append($("<div />")
                      .addClass("mybox")
                      .append($('<a/>', {  
                          href: $(this).find("customer_link").text(),  
                          target: "_blank",  
                          text: $(this).find("customer_company").text()  
                      })));

The CDATA in your xml is going to cause jQuery problems because it is parsing the xml like it's html, not according to the xml specs. You may be better off parsing your xml with a plugin like jParse if you can't remove the CDATA and <br> from the customer_company element.

想念有你 2024-10-31 18:35:56

看一下您的代码片段:

$("#guys").append("<div class="mybox">.......

您正在关闭标签,而您可能无意这样做:

尝试一下:

$("#guys").append("<div class='mybox'>.......

不过,您需要更仔细地考虑一下,因为稍后您的字符串中会有一些引号。

Have a look at a snippet of your code:

$("#guys").append("<div class="mybox">.......

You're closing the tags when you possibly don't mean to:

Try this:

$("#guys").append("<div class='mybox'>.......

You'll need to think a little more carefully, though, as you have some quotes in your string later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文