简体   繁体   中英

What kind of JavaScript is this?

I have an application that has this format scattered around but I dont know what kind it is. It's not jQuery, so what is it?

$('some_edit').style.display  = "block";
$('some_views').style.display = "none";

I get this in firebug and I know the element is present:

$("some_edit").style is undefined

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

A lot of people have defined the '$' symbol as a substitute for document.getElementById().

Basically:

function $(id) { return document.getElementById(id); }
$("ElementID").innerHTML = "Text"; //Usage

A more proper, "namespace" example:

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
        // ...
    }
}

I have used a self-invocation pattern ( function(){ ... }() ) in this example.

at first i thought the jquery selector would likely have been $("#some_edit") and then .css() . so I would have said, prototype or mootools or a home brew $.

you can certainly discount both mootools and prototype , because if the selector returns an object, then the style property will be available (ignoring best practices in both frameworks on setting styles).

this leaves, the site uses homebrew $ assignment or jquery, which is not being used correctly.

actually, $("foo").style.blah in jquery will produce this very exception (even if the selector was good) - here is jsfiddle to the rescue

case point jquery (triggers): http://www.jsfiddle.net/dimitar/vmsZn/

case point prototype (works): http://www.jsfiddle.net/dimitar/vmsZn/1/

case point mootools (works): http://www.jsfiddle.net/dimitar/vmsZn/2/

It is setting the display style for the two page elements - the display property specifies the type of box an element should generate.

block = The element will generate a block box (a line break before and after the element) none = The element will generate no box at all

Put a [0] in front of $('some_views') to return the Native DOM Element.

$('some_views')[0].style.display = "none";

or $('some_views').get(0).style.display = "none";

or $('some_views').css('display', 'none') to iterate through the collection of DOM elements.

It's JQuery -- uses $ as its key variable.

Added:

Could also be mootools. Also uses $

Added:

'some_edit' would be the id of an element.

ps. I agree $ could be anything. Odds are though that it is JQuery or Mootools. "When you hear hoof beats, think horses, not zebras."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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