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

JavaScript dollar function, function $() error - Stack Overflow

programmeradmin0浏览0评论

I've e across the dollar sign function over the internets and decided to use it for a javascript toggle menu. However, the "$" symbol makes my code fail.

This is what I'm trying to use:

function $() {
        var elements = new Array();
        for (var i = 0; i < arguments.length; i++) {
            var element = arguments[i];
            if (typeof element == 'string')
                element = document.getElementById(element);
            if (arguments.length == 1)
                return element;
            elements.push(element);
        }
        return elements;
    }

    function toggle(obj) {
        var el = $(obj);
        el.style.display = (el.style.display != 'none' ? 'none' : '' );
    }

The $ from "function $(){" seems to break the code. How do you declare this function? If I replace $ with "anything", it works, but not as a dollar function...

I've e across the dollar sign function over the internets and decided to use it for a javascript toggle menu. However, the "$" symbol makes my code fail.

This is what I'm trying to use:

function $() {
        var elements = new Array();
        for (var i = 0; i < arguments.length; i++) {
            var element = arguments[i];
            if (typeof element == 'string')
                element = document.getElementById(element);
            if (arguments.length == 1)
                return element;
            elements.push(element);
        }
        return elements;
    }

    function toggle(obj) {
        var el = $(obj);
        el.style.display = (el.style.display != 'none' ? 'none' : '' );
    }

The $ from "function $(){" seems to break the code. How do you declare this function? If I replace $ with "anything", it works, but not as a dollar function...

Share Improve this question edited Oct 11, 2016 at 18:52 CerebralFart 3,4905 gold badges28 silver badges31 bronze badges asked Jan 26, 2011 at 19:22 SpectraljumpSpectraljump 4,67711 gold badges47 silver badges61 bronze badges 2
  • do you also use jquery or some other javascript framework ? – Gabriele Petrioli Commented Jan 26, 2011 at 19:26
  • Can you explain why you feel '$' is a good name for this function? – ChaosPandion Commented Jan 26, 2011 at 19:30
Add a ment  | 

2 Answers 2

Reset to default 8

The dollar sign is not a standard Javascript function, but is part of a third party library.

There are two well-known libraries which use the dollar sign in this way.

The older one is called Prototype, but the one which is currently in vogue, and most likely to be the one you've seen in use is JQuery.

Both these libraries would be used by adding a <script> tag to your HTML page, to include the library code, after which you can use their functionality.

Most of the functionality of both these libraries is contained within their respective $() functions. In the case of JQuery, you can also refer to the $() function as jQuery() to prevent namespace clashes, in the event that you wanted to use both of them.

I suggest reading up on JQuery before continuing -- JQuery is very powerful, and adds a lot of functionality, but the coding style for writing JQuery code can be quite different from regular Javascript, and can take a bit of getting used to. And that's quite apart from learning the API and finding out what it can do.

To actually answer your question -- which is how to declare $ as a function name, I suggest having a look at the JQuery source code to see how they do it. However, I managed to produce a working $() function first time I tried, like this:

var $ = function() {alert('dollar works for me');}
$();

But to be honest, I wouldn't do that. If you really want to use the $() function in the way it's being used in other sites, you need to use JQuery. It does a whole lot more than just wrapping document.getElementById().

By the way, JQuery and Prototype are not the only similar libraries out there. If you're interested in this sort of thing, you may also want to look into MooTools, YUI, and a few others.

Hope that helps.

The $ sign is a notation for various javascript frameworks (prototype/jQuery). Since replacing it with "anything else" works, you most likely have a clash between that inline function and the framework you are using.

In itself, the notation and function is correct, as the following example shows.

Open a new tab/window and enter this on the address bar:

javascript:eval("function $() { alert('hi'); } $();");
发布评论

评论列表(0)

  1. 暂无评论