当前位置: 动力学知识库 > 问答 > 编程问答 >

Good way to use custom object properties and methods for working with jquery selectors

问题描述:

I have the following code:

$(document).on('click', '#baseRegisterListContainer table thead th[data-isSortable]', function () { ... }

And it works fine. But all the functionality of the page I've gathered inside one object:

$.BaseRegisterList = function () {

this.getForm = function () {

return $('#baseRegisterListForm');

...and so on

}

}

How to organize the object to use selector results as well as selector strings like above, only from within this object?

And eliminate hardcoded lines as above within js file?

网友答案:

I came up with the following:

$.BaseRegisterList = function () {
this.form = '#baseRegisterListForm';
this.table = '#baseRegisterListContainer table';
this.getRows = function () {
    return $(this.table + ' tbody tr');
}
this.moreLinkButton = $(this.table + ' tfoot a').first();
this.sortableColumns = this.table + ' thead th[data-isSortable]';
this.getSortedColumn = function () {
    return $(this.sortableColumns + '[data-sortOrder]').eq(0);
}
//so on

So if i need an object instead of string - I just embrace it with $.

分享给朋友:
您可能感兴趣的文章:
随机阅读: