这是什么类型的JavaScript?

Mat*_*iby 8 javascript jquery mootools

我有一个应用程序,这种格式散落,但我不知道它是什么类型.它不是jQuery,所以它是什么?

$('some_edit').style.display  = "block";
$('some_views').style.display = "none";
Run Code Online (Sandbox Code Playgroud)

我在firebug中得到这个,我知道元素存在:

$("some_edit").style is undefined
Run Code Online (Sandbox Code Playgroud)

Mar*_*arz 16

它可能是很多东西 - 检查源代码(或使用Firebug)并查看正在加载的JS库.

  • 如果您打算使用Firebug,也可以设置一个断点并进入它,以便更加确定正在执行哪个库. (2认同)

pal*_*wim 12

很多人已经将'$'符号定义为document.getElementById()的替代品.

基本上:

function $(id) { return document.getElementById(id); }
$("ElementID").innerHTML = "Text"; //Usage
Run Code Online (Sandbox Code Playgroud)

一个更恰当的"命名空间"示例:

var DOM = { // creating the namespace "DOM"
    $: (function() {
        if(document.getElementById)
            return function(id){ return document.getElementById(id); }
        else if(document.all)
            return function(id) { return document.all[id]; }
        else
            return function(id) { /* I don't even want to get into document.layers */ }
    })()
};

// Later in the code:
{
    function ExampleFunction() {
        // ...
        DOM.$("ElementID").style.backgroundColor = "#96d0a0"; // a nice minty green color
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这个例子中使用了自调用模式(function(){ ... }()).