开发者

How to translate such a function to jQuery?

开发者 https://www.devze.com 2023-02-20 13:58 出处:网络
So in my code I used function $(id) { return document.getElem开发者_开发技巧entById(id); } I want to move to jQuery. How to translate such thing to it?Don\'t use this function, function $(id). Use

So in my code I used

function $(id) {
    return document.getElem开发者_开发技巧entById(id);
}

I want to move to jQuery. How to translate such thing to it?


Don't use this function, function $(id). Use the jQuery function $('#id'). This function will return an object with bunch of jQuery methods.

Methods include remove(), hide(), toggle(), etc., and the implementation is like $('#id').hide(), $('#id').show(), etc.

There are so many, many jQuery methods, that simplifies it in so many ways.


If I got your question right, you will need to include jQuery inside your page, and replace the following line:

return document.getElementById(id);

with this one:

return $("#"+id);


Using Jquery selectors you can do the same thing very easily.

$("#" + id)

For more details:

http://api.jquery.com/id-selector/


Just use

$("#"+id)

That will return the element with the right ID.

0

精彩评论

暂无评论...
验证码 换一张
取 消