使用jQuery选择自定义类似fbml的标签

时间:2011-01-28 05:55:38

标签: jquery jquery-selectors fbml

FBML正在使用<fb:like></fb:like> <fb:board></fb:board>等标签。

他们如何选择这些?我尝试过$('fb')$('fb:like'),都返回一个空对象...有谁知道如何做到这一点?

2 个答案:

答案 0 :(得分:13)

例如,

<fb:like href="http://google.com" layout="box_count" show_faces="false" width="450"></fb:like>


quoted

/*

If you wish to use any of the meta-characters 
( such as !"#$%&'()*+,./:;?@[\]^`{|}~ ) as a literal part of a name, 
you must escape the character with two backslashes: \\. For example, 
if you have an an element with id="foo.bar", you can use the selector 
$("#foo\\.bar"). 

*/

demo

答案 1 :(得分:0)

您的问题可能是jQuery没有使用DOM选择器的“* NS”风格(例如getElementsByTagNamegetElementsByTagNameNS)。

我刚才写了一篇hack来为jQuery提供这种功能(特别是对于XHTML上下文)。您可以根据自己的需要定制它:

https://gist.github.com/352210

/**
* Hack to allow jQuery to work within XHTML documents that define an xmlns
*/

/**
* Use the given object to override the given methods in its prototype
* with namespace-aware equivalents
*/
function addNS(obj, methods) {
    var proto = obj.constructor.prototype;
    var ns = document.documentElement.namespaceURI;

    for (var methodName in methods) {
        (function () {
            var methodNS = proto[methodName + "NS"];

            if (methodNS) {
                proto[methodName] = function () {
                    var args = Array.prototype.slice.call(arguments, 0);
                    args.unshift(ns);
                    return methodNS.apply(this, args);
                };
            }
        })();
    }
}

// Play nice with IE -- who doesn't need this hack in the first place
if (document.constructor) {
    // Override document methods that are used by jQuery
    addNS(document, {
        createElement: 1,
        getElementsByTagName: 1
    });

    // Override element methods that are used by jQuery
    addNS(document.createElement("div"), {
        getElementsByTagName: 1,
        getAttribute: 1,
        getAttributeNode: 1,
        removeAttribute: 1,
        setAttribute: 1
    });
}