【问题标题】:How do I make a tag in the body to properly show如何在正文中制作标签以正确显示
【发布时间】:2013-09-30 07:37:05
【问题描述】:
xml = '<?xml version="1.0" encoding="utf-8"?><root><html><a href="#">the new link</a></html></root>';

这些变量是我的

 $(function(){
xml = '<?xml version="1.0" encoding="utf-8"?><root><html><a href="#">the new link</a></html></root>';
$xmls =$($.parseXML( xml ));

$('button').on('click',function(){
    console.log($xmls.find('html > *')[0]);
    $('body').append($xmls.find('html > *')[0]);
});
})

添加了一个标签不起作用

pre标签会显示为A标签

http://jsfiddle.net/GgXEp/1/

【问题讨论】:

标签: javascript jquery xml xml-parsing


【解决方案1】:

DEMO

我修好了。它适用于每个浏览器。我花了一些时间,所以我希望它有效。 我还在 xml 文档中添加了另一个链接,以使其仅选择索引为 0 的元素:

$('button').on('click',function(){
    HTML = $($.parseHTML(xmls.find('root').text()));
    console.log(HTML[0]);
    $('body').append(HTML[0]);
});

【讨论】:

  • 如果你从 jquery 1.10.1 迁移到 1.9.1 可以正常工作
  • @SnakeEyes 非常感谢
【解决方案2】:

经过1小时的处理和思考,我将为您提供一个解决方法:

首先,将 jquery 库更新为 1.9.1(可能是 1.10.1 的问题,因为控制台在 IE 和 Opera 中拒绝访问),然后将找到的元素存储在 jquery 变量中,例如:

$element = xmls.find('html > *').first();

然后让我们使用 jquery 创建一个锚点:

$a = $("<a>").attr( "href", $element.attr("href") ).text( $element.text() );

之后,让我们在点击动作的按钮中添加:

$('button').on('click', function () {
   $('body').append($a);
});

当然,证明:Jsfiddle

猜你喜欢
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
相关资源
最近更新 更多