Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用XML文档中的内容更新链接文本_Javascript_Jquery_Xml_Ajax - Fatal编程技术网

Javascript 使用XML文档中的内容更新链接文本

Javascript 使用XML文档中的内容更新链接文本,javascript,jquery,xml,ajax,Javascript,Jquery,Xml,Ajax,对于每个链接,例如 <a href="http://www.wowhead.com/item=78363">http://www.wowhead.com/item=78363</a> 在XML页面中,我需要CDATA部分中的值,如下所示: <name> <![CDATA[Vagaries of Time]]> </name> 这就是“时间的变幻莫测”。然后我需要在标记中插入名称: <a href="http://www.w

对于每个链接,例如

<a href="http://www.wowhead.com/item=78363">http://www.wowhead.com/item=78363</a>
在XML页面中,我需要
CDATA
部分中的值,如下所示:

<name>
<![CDATA[Vagaries of Time]]>
</name>

这就是“时间的变幻莫测”。然后我需要在标记中插入名称:

<a href="http://www.wowhead.com/item=78363">Vagaries of Time</a>


如何实现这一点?

基于正则表达式循环链接,发送Ajax请求,使用正则表达式解析文本,然后完成

$('a').each(function() {
    var $this = $(this);

    if(/item=\d+$/.test(this.href)) {
        $.ajax({
            url: this.href + '&xml',
            dataType: 'text',
            success: function(data) {
                $this.text(/<!\[CDATA\[([^\]]+)/.exec(data)[1]);
            }
        });
    }
});
$('a')。每个(函数(){
var$this=$(this);
if(/item=\d+$/.test(this.href)){
$.ajax({
url:this.href+'&xml',
数据类型:“文本”,
成功:功能(数据){
$this.text(/

当然,你很可能想添加一些错误检查。如果你的XML文档比你发布的例子复杂得多,请考虑使用原生XML能力来解析。我只是在这里使用了一个简单的表达式。

AGH!我的眼睛!请保持格式化到标准水平,谢谢。对不起,我的英文是不好,无论如何谢谢你的编辑。或者其他一些想法?也许在xml页面和脚本之间粘贴一个带有代码的php文件,然后将根据脚本的请求获取数据?
/get_xml.php?id=78363
?等。xml页面是实时的:(我们获取数据的站点不是我的).在这个XML页面上有更多选项CDATA:-/@Edoras:好的,你需要用你自己的服务器代理这个页面;跨源策略不允许你直接通过Ajax向它发送请求。至于另一个CDATA,你需要的是第一个,所以没问题。哦,如果我理解的话-当XML不是我的页面,我不知道代理服务r、 那么它不起作用了(
$('a').each(function() {
    var $this = $(this);

    if(/item=\d+$/.test(this.href)) {
        $.ajax({
            url: this.href + '&xml',
            dataType: 'text',
            success: function(data) {
                $this.text(/<!\[CDATA\[([^\]]+)/.exec(data)[1]);
            }
        });
    }
});