最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - What kind of JavaScript is this? - Stack Overflow

programmeradmin2浏览0评论

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

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
Share Improve this question edited Nov 4, 2010 at 22:28 palswim 12.1k8 gold badges56 silver badges79 bronze badges asked Aug 13, 2010 at 18:09 Matt ElhotibyMatt Elhotiby 44.1k91 gold badges224 silver badges328 bronze badges 1
  • 4 I'd say there's a good chance it is prototypejs. That's how you get elements by ID. jsfiddle.net/Ntwwz – user113716 Commented Aug 13, 2010 at 18:17
Add a comment  | 

6 Answers 6

Reset to default 16

It could be many things - examine the source code (or use Firebug) and see what JS libraries are being loaded.

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."

发布评论

评论列表(0)

  1. 暂无评论