将代码与jQuery合并

将代码与jQuery合并,jquery,Jquery,难道没有更好的方法来编写这段代码吗?在某种程度上,我可以创建一个按钮add,将其添加到filter div类中,并使用额外的css和on-click功能 $('.dataTables_filter').parent().append('<button class="btn btn-primary" id="addnew">Add New</button>'); $('#addnew').css('margin-right', '20px').css('margin-bo

难道没有更好的方法来编写这段代码吗?在某种程度上,我可以创建一个按钮add,将其添加到filter div类中,并使用额外的css和on-click功能

$('.dataTables_filter').parent().append('<button class="btn btn-primary" id="addnew">Add New</button>');

$('#addnew').css('margin-right', '20px').css('margin-bottom', '5px').css('float', 'right').on('click', function(e) {
    e.preventDefault();
    window.location.replace(window.location.href + '/create');
});
$('.dataTables_filter').parent().append('addnew');
$('#addnew').css('margin-right','20px').css('margin-bottom','5px').css('float','right').on('click',函数(e){
e、 预防默认值();
window.location.replace(window.location.href+'/create');
});
首先,
css()
接受一个对象,因此您可以在一次调用中添加多个属性:

$foo.css({
    'margin-right': '20px',
    'margin-bottom': '5px',
    'float': 'right'
});
然而,这仍然不理想,因为这意味着您的JS代码中有CSS逻辑。最好的解决方案是将规则设置为CSS类,并根据需要添加/删除该类:

.foo {
    margin-right: 20px,
    margin-bottom: 5px,
    float: right;
}
$foo.addClass('foo');
关于第一个问题,您可以动态创建
按钮
元素,并立即创建其“click”处理程序,如下所示:

var $button = $('<button />', { class: 'btn btn-primary', id: 'addnew', text: 'Add New' }).click(function(e) {
    e.preventDefault();
    window.location.replace(window.location.href + '/create');
}).appendTo($('.dataTables_filter').parent());
首先,
css()
接受一个对象,因此您可以在一次调用中添加多个属性:

$foo.css({
    'margin-right': '20px',
    'margin-bottom': '5px',
    'float': 'right'
});
然而,这仍然不理想,因为这意味着您的JS代码中有CSS逻辑。最好的解决方案是将规则设置为CSS类,并根据需要添加/删除该类:

.foo {
    margin-right: 20px,
    margin-bottom: 5px,
    float: right;
}
$foo.addClass('foo');
关于第一个问题,您可以动态创建
按钮
元素,并立即创建其“click”处理程序,如下所示:

var $button = $('<button />', { class: 'btn btn-primary', id: 'addnew', text: 'Add New' }).click(function(e) {
    e.preventDefault();
    window.location.replace(window.location.href + '/create');
}).appendTo($('.dataTables_filter').parent());
首先,
css()
接受一个对象,因此您可以在一次调用中添加多个属性:

$foo.css({
    'margin-right': '20px',
    'margin-bottom': '5px',
    'float': 'right'
});
然而,这仍然不理想,因为这意味着您的JS代码中有CSS逻辑。最好的解决方案是将规则设置为CSS类,并根据需要添加/删除该类:

.foo {
    margin-right: 20px,
    margin-bottom: 5px,
    float: right;
}
$foo.addClass('foo');
关于第一个问题,您可以动态创建
按钮
元素,并立即创建其“click”处理程序,如下所示:

var $button = $('<button />', { class: 'btn btn-primary', id: 'addnew', text: 'Add New' }).click(function(e) {
    e.preventDefault();
    window.location.replace(window.location.href + '/create');
}).appendTo($('.dataTables_filter').parent());
首先,
css()
接受一个对象,因此您可以在一次调用中添加多个属性:

$foo.css({
    'margin-right': '20px',
    'margin-bottom': '5px',
    'float': 'right'
});
然而,这仍然不理想,因为这意味着您的JS代码中有CSS逻辑。最好的解决方案是将规则设置为CSS类,并根据需要添加/删除该类:

.foo {
    margin-right: 20px,
    margin-bottom: 5px,
    float: right;
}
$foo.addClass('foo');
关于第一个问题,您可以动态创建
按钮
元素,并立即创建其“click”处理程序,如下所示:

var $button = $('<button />', { class: 'btn btn-primary', id: 'addnew', text: 'Add New' }).click(function(e) {
    e.preventDefault();
    window.location.replace(window.location.href + '/create');
}).appendTo($('.dataTables_filter').parent());

我想您可以将代码精简为:

$('.dataTables_filter').parent().append(
    '<button class="btn btn-primary" style="margin-right: 20x; margin-bottom: 5px; float: right;" onclick="window.location.replace(window.location.href + \"/create\"); return false" id="addnew">Add New</button>'
);
$('.dataTables_filter').parent().append(
“添加新的”
);

我想您可以将代码精简为:

$('.dataTables_filter').parent().append(
    '<button class="btn btn-primary" style="margin-right: 20x; margin-bottom: 5px; float: right;" onclick="window.location.replace(window.location.href + \"/create\"); return false" id="addnew">Add New</button>'
);
$('.dataTables_filter').parent().append(
“添加新的”
);

我想您可以将代码精简为:

$('.dataTables_filter').parent().append(
    '<button class="btn btn-primary" style="margin-right: 20x; margin-bottom: 5px; float: right;" onclick="window.location.replace(window.location.href + \"/create\"); return false" id="addnew">Add New</button>'
);
$('.dataTables_filter').parent().append(
“添加新的”
);

我想您可以将代码精简为:

$('.dataTables_filter').parent().append(
    '<button class="btn btn-primary" style="margin-right: 20x; margin-bottom: 5px; float: right;" onclick="window.location.replace(window.location.href + \"/create\"); return false" id="addnew">Add New</button>'
);
$('.dataTables_filter').parent().append(
“添加新的”
);

你可以,但你会把所有的最佳实践都抛在脑后。这些最佳实践是什么?它简洁明了,唯一的缺点是缺乏可重用性。但是三分之二也不错……内联CSS的使用,除了丑陋之外,还会让维护成为一场噩梦,而且不是很枯燥。类似地,
onclick
的使用已经过时,不能很好地分离关注点。你可以这样做,但你会把所有最佳实践都抛在脑后。这些最佳实践是什么?它简洁明了,唯一的缺点是缺乏可重用性。但是三分之二也不错……内联CSS的使用,除了丑陋之外,还会让维护成为一场噩梦,而且不是很枯燥。类似地,
onclick
的使用已经过时,不能很好地分离关注点。你可以这样做,但你会把所有最佳实践都抛在脑后。这些最佳实践是什么?它简洁明了,唯一的缺点是缺乏可重用性。但是三分之二也不错……内联CSS的使用,除了丑陋之外,还会让维护成为一场噩梦,而且不是很枯燥。类似地,
onclick
的使用已经过时,不能很好地分离关注点。你可以这样做,但你会把所有最佳实践都抛在脑后。这些最佳实践是什么?它简洁明了,唯一的缺点是缺乏可重用性。但是三分之二也不错……内联CSS的使用,除了丑陋之外,还会让维护成为一场噩梦,而且不是很枯燥。类似地,
onclick
的使用已经过时,不能很好地分离关注点。