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

jquery - How to define function for a dom object in javascript - Stack Overflow

programmeradmin5浏览0评论

This is a part of a more broad question. It is getting low attention, so let me please ask the only part of it that I can't implement myself. How can I register jquery-like javascript function for a dom object? Say i have a following html page:

<html><body>
  <div id = "table"/>
  <div id = "chart"/>
</body></html>

and want to be able to call $('#table').update() and $('#chart').update()? I need those update functions to contain different logic and local variables, for example different url to load data from. Sorry for probably being noob.

UPDATE

If I understand correctly, plugin is a function in a global namespace that can process any object. I'd rather want to associate different functions with different elements. That because I thought that it would be easier to associate different update functions with different objects, than write one update function which for every object has to investigate is it applicable, and if yes, how.

This is a part of a more broad question. It is getting low attention, so let me please ask the only part of it that I can't implement myself. How can I register jquery-like javascript function for a dom object? Say i have a following html page:

<html><body>
  <div id = "table"/>
  <div id = "chart"/>
</body></html>

and want to be able to call $('#table').update() and $('#chart').update()? I need those update functions to contain different logic and local variables, for example different url to load data from. Sorry for probably being noob.

UPDATE

If I understand correctly, plugin is a function in a global namespace that can process any object. I'd rather want to associate different functions with different elements. That because I thought that it would be easier to associate different update functions with different objects, than write one update function which for every object has to investigate is it applicable, and if yes, how.

Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Oct 28, 2013 at 14:03 latheierelatheiere 4514 silver badges14 bronze badges 5
  • 2 Is update() a plugin you've written? You could use data-* variables on the elements containing the differing data, and process the logic on them in your update() method. – Rory McCrossan Commented Oct 28, 2013 at 14:06
  • Are you asking how to create a plugin? – George Commented Oct 28, 2013 at 14:06
  • I do not know whether plugin is a solution, and updated my question accordingly – latheiere Commented Oct 28, 2013 at 14:15
  • 1 "jquery-like javascript function for a dom object" You are not calling DOM object methods, but jQuery functions in your example: $('#table').update(). To call a DOM object method you will need to access the DOM object first: $('#table')[0].update() or $('#table').item(0).update(). See: api.jquery./Types/#jQuery jQuery plugin functions can be created/appended using the extend function as @James Donnelly suggests; to add a DOM method you must set it to the objects prototype. – feeela Commented Oct 28, 2013 at 14:19
  • thanks @feela I think your ment is worth being an answer, it clarified me why some of my attempts failed – latheiere Commented Oct 28, 2013 at 14:30
Add a ment  | 

3 Answers 3

Reset to default 6

What you're after is jQuery's fn.extend():

$.fn.extend({
    update: function() {
        /* Required code. */
    }
});

Then you can simply call .update() on a jQuery object to execute that function:

$('myElement').update();

As an example use, if we wanted to log the id of an element, we could define our update() function as:

$.fn.extend({
    update: function() {
        console.log("ID = " + this.id);
    }
});

Then call:

$('#table').update();

Which would log:

ID = table

You don't need jQuery for this. DOM elements are objects, so you can give them any methods you want:

var table = document.getElementById('table');
table.update = function() {
  this.innerHTML += 'table updated ';
}.bind(table);

var chart = document.getElementById('chart');
chart.update = function() {
  this.innerHTML += 'chart updated ';
}.bind(chart);


document.getElementById('table').update();
document.querySelector('#chart').update();

Example: http://jsbin./uReyIRi/1/edit

You can add new methods to the DOM objects via their prototypes.

/* extend existing prototype */
HTMLTable.prototype.update = function() {
    console.log( this );
}

/* call new method */
document.querySelector( 'table' ).update();

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype

发布评论

评论列表(0)

  1. 暂无评论