简体   繁体   English

jQuery Selector转义冒号“:”在IE中有效但在其他地方不起作用

[英]jQuery Selector escaping colon “:” works in IE but does not work elsewhere

I have an XSL object. 我有一个XSL对象。 I want to extract a select group of nodes. 我想提取一组选定的节点。 Another person or team wrote the XSL document and the line of code which selects those nodes, but it only works in IE and I'm trying to make it cross-browser compatible. 另一个人或团队编写了XSL文档和选择这些节点的代码行,但它只适用于IE,我试图使其跨浏览器兼容。

XSL file XSL文件

<!-- snippet of XSL file -->
<xsl:variable name="title">aaa</xsl:variable>

<xsl:variable name="col1">CSV00001</xsl:variable>
<xsl:variable name="col2">CSV00002</xsl:variable>
<xsl:variable name="col3">CSV00003</xsl:variable>
<xsl:variable name="col4">CSV00004</xsl:variable>
<xsl:variable name="col5">CSV00005</xsl:variable>
<xsl:variable name="col6">CSV00006</xsl:variable>
<xsl:variable name="col7">CSV00007</xsl:variable>
<xsl:variable name="col8">CSV00008</xsl:variable>
<xsl:variable name="col9">CSV00009</xsl:variable>

JS file JS文件

// Extract title and column nodes
var varNodes = $(csvXsl).find("xsl\\:variable");

I tried referencing this article for ideas, Handling a colon in an element ID in a CSS selector , but that is for CSS. 我尝试引用本文的想法, 在CSS选择器中处理元素ID中的冒号 ,但这是针对CSS的。 But I did attempt to replace the \\: with the \\\\3A , \\\\3a , 3a , 3A and none of those worked. 但我确实尝试用\\\\3A\\\\3a3a3A替换\\:没有这些工作。

Any suggestions? 有什么建议么?

As requested, here is a litle more info on how the XSL object gets created. 根据要求,这里有关于如何创建XSL对象的更多信息。

function loadXml(xmlFilePath) {
    var retObj = null ;
    var timeUniq = (new Date()).getTime();

    // check protocol
    var protocol = document.location.protocol;
    if(protocol.toLowerCase().indexOf("http")>=0){
        $.ajax({
            url: xmlFilePath + "?t=" + timeUniq,
            type: 'get',
            dataType: 'xml',
            async: false,
            timeout: 1000
        })
           .done(
               function(xml, status){
                   if( status != 'success' ){
                       return;
                   }
                   retObj = xml;
               })
           .fail(
               function(xhr, textStatus){
                   return;
               });
    }

    return retObj;
}

csvXsl = loadXml("./xsl/OrderListCSV.xsl");

Thank you. 谢谢。

Update 更新

As requested, I have added a fiddle. 根据要求,我添加了一个小提琴。

http://jsfiddle.net/Q3jNN/ http://jsfiddle.net/Q3jNN/

Update2 UPDATE2

Working JSFiddle routine with all the credit to commentator Niet. 使用JSFiddle例程,所有功能都归功于评论员Niet。

http://jsfiddle.net/dNDN6/2/ http://jsfiddle.net/dNDN6/2/

In case the link is broken, here is the relevant portion: 如果链接断开,这里是相关部分:

// Workaround for JQuery bug, primarily using Vanilla JS.

function serializeXmlNode(xmlNode) {
    if (typeof window.XMLSerializer != "undefined") {
        return (new window.XMLSerializer()).serializeToString(xmlNode);
    } else if (typeof xmlNode.xml != "undefined") {
        return xmlNode.xml;
    }
    return "";
}

var tmpStr = serializeXmlNode(csvXsl); // Converts XML/XSL object to String
var tmp = document.createElement('div');

tmp.innerHTML = csvXsl; // Does not work
tmp.innerHTML = tmpStr; // This one works, had to serialize object first

var varNodes = tmp.getElementsByTagName('xsl\:variable'); // Works
var varNodes2 = tmp.getElementsByTagName('xsl:variable'); // Also works

I would do a number of steps to debug this issue: 我会做很多步骤来调试这个问题:

  1. Check the content of the csvXls variable, you can try for example to look for: console.log(csvXsl) then $(csvXsl).find("xsl\\\\:variable"); 检查csvXls变量的内容,你可以尝试例如查找: console.log(csvXsl)然后$(csvXsl).find("xsl\\\\:variable"); or $("xsl\\\\:variable",csvXsl); $("xsl\\\\:variable",csvXsl);

  2. If that variable contains the correct nodes, for example <xsl:variable name="col9">CSV00009</xsl:variable> then it could be a bug in jQuery, which I feel to exclude in such a basic selector. 如果该变量包含正确的节点,例如<xsl:variable name="col9">CSV00009</xsl:variable>那么它可能是jQuery中的一个错误,我觉得要在这样的基本选择器中排除。

  3. Post how do you get the csvXsl with a bit more context, so we can help you better. 发布如何获得更多上下文的csvXsl ,以便我们更好地帮助您。

This is indeed a bug in jQuery. 这确实是jQuery中的一个错误。

Try using Vanilla JS instead: 尝试使用Vanilla JS代替:

var tmp = document.createElement('div');
tmp.innerHTML = csvXsl;
var varNodes = tmp.getElementsByTagName('xsl:variable');

must comment by 'Answer' since not enough reps 必须通过'回答'评论,因为没有足够的代表

the line: 这条线:

 var inputTag = $("#test\\:1").val()); 

in the fiddle supposedly showing what works : http://jsfiddle.net/dNDN6/2/ 在小提琴据说显示什么有效: http//jsfiddle.net/dNDN6/2/

actually does not work in Chrome / webkit 实际上在Chrome / webkit中不起作用

the correct method is to use plain JS and getElementsByTagName first on the tag without the namespace (after the colon) and if no element then the full tag like so: 正确的方法是首先在没有命名空间(冒号后)的标签上使用普通JS和getElementsByTagName,如果没有元素,则使用完整标签,如下所示:

var x = xmlDoc.getElementsByTagName("variable");
if (!x[0]) x = xmlDoc.getElementsByTagName('xsl:variable');

x will contain your array [cross-browser] x将包含你的数组[跨浏览器]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM