使用javascript向列表中的第一项添加类
我对此有意见使用javascript向列表中的第一项添加类,javascript,jquery,Javascript,Jquery,我对此有意见 我想在我们的分类页面的列表中的第一个项目周围添加边框,但只在第一个页面上添加边框,而不是在后面的页面上添加边框。我这样做对吗?我无法让它工作,而且似乎这会工作 尝试一下你可能要做的事情,见下文。此外,我将从DIV中删除内联样式,并为其提供一个类,将样式移动到样式表中。这条路更干净,更容易维护 话虽如此,这里有一个建议: if ((location.search.indexOf('page=') < 0) || (location.search.indexOf('page=1'
我想在我们的分类页面的列表中的第一个项目周围添加边框,但只在第一个页面上添加边框,而不是在后面的页面上添加边框。我这样做对吗?我无法让它工作,而且似乎这会工作 尝试一下你可能要做的事情,见下文。此外,我将从DIV中删除内联样式,并为其提供一个类,将样式移动到样式表中。这条路更干净,更容易维护 话虽如此,这里有一个建议:
if ((location.search.indexOf('page=') < 0) || (location.search.indexOf('page=1') > 0) {
if (($('select.sort-by option:selected').text() == "Most Popular") || ($('select.sort-by option:selected').text() == "Best Selling")) {
$('li.collection-indv-product:first').css('border', '#ffc000 solid 10px');
$("li.collection-indv-product:first").find('div.collection-product').before('<div style="background:#ffc000;text-align:left;padding: 0 0 5px 5px;color:#353535;font-weight:bold;font-size:1.3em;">Customer Favorite in {{collection.title}}</div>');
}
}
第1页是第1页还是第0页?另外,您能否提供一个您试图匹配的标记的示例?您是在尝试匹配第一个子项还是第一个找到的项周期?我猜您需要使用parseInt来获得所需的结果。
$(function () {
function getPage() {
var query = location.search;
if (query != '') {
// this gets 'page' anywhere in the querystring
var qs = query.substr(1).split('&');
for (k in qs) {
if (qs[k].indexOf('page') == 0) {
var page = qs[k].split('=')[1];
return page;
}
}
}
return null; // page wasn't found
}
if (getPage() == '1') {
var sort = $('select.sort-by :selected').val();
if (sort == 'Most Popular' || sort == 'Best Selling') {
// use .first() and chaining for performance
var li = $('li.collection-indv-product').first();
li.css('border', '#ffc000 solid 10px')
.find('div.collection-product').before('<div style="background:#ffc000;text-align:left;padding: 0 0 5px 5px;color:#353535;font-weight:bold;font-size:1.3em;"/>').text('Customer Favorite in {{collection.title}}');
;
}
}
});