将链接添加到 xml 标记并使用 jquery 解析它
我可以用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在混合引用样式。单引号 (
'
) 字符串可以包含未转义的双引号 ("
),反之亦然。在双引号字符串中包含未转义的双引号会破坏您的代码。您可以看看它如何创建随机未定义的引用,例如
mybox
,但不会构建您想要的字符串。试试这个:
或者,您可以使用 jQuery 构建所有元素,并完全避免在字符串中嵌入带引号的属性:
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.You can see how this creates random undefined references like
mybox
, but doesn't build the string you intended.Try this:
Alternatively, you can use jQuery to build all the elements and completely avoid having attributes with quotes embedded in your string:
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 theCDATA
and<br>
from thecustomer_company
element.看一下您的代码片段:
您正在关闭标签,而您可能无意这样做:
尝试一下:
不过,您需要更仔细地考虑一下,因为稍后您的字符串中会有一些引号。
Have a look at a snippet of your code:
You're closing the tags when you possibly don't mean to:
Try this:
You'll need to think a little more carefully, though, as you have some quotes in your string later.