我有一個jQuery搜索腳本,它使用選項卡爲用戶定義他們要使用的搜索類型。當用戶搜索時,創建一個類似於#類型的網址/查詢 /。如果#類型位於URL中,則與該類型對應的選項卡顯示爲選中狀態。但是,如果查詢也在url中(#類型/查詢 /)該選項卡未被選中。用我的代碼下面,我怎麼能做到這一點,所以它會忽略/ 查詢 /?後的一切。我希望你能理解我想要描述的東西。在jQuery選項卡腳本中通過url定義選定的選項卡
我的jQuery腳本是:
$(document).ready(function() {
$('[id^=type_]').click(function() {
type = this.id.replace('type_', '');
$('[id^=type_]').removeClass('selected');
$('#type_' + type).addClass('selected');
return false;
});
if (window.location.hash != "") {
url = window.location.hash.replace('#', '');
$('#type_' + url).click();
} else {
$('#type_search').click();
}
$('#query').keyup(function() {
var query = $(this).val();
var url = '/' + type + '/' + query + '/';
window.location.hash = '' + type + '/' + query + '/';
document.title = $(this).val() + ' - My Search';
$('#results').show();
if (query == '') {
window.location.hash = '';
document.title = 'My Search';
$('#results').hide();
}
$.ajax({
type: 'GET',
url: url,
dataType: 'html',
success: function (results) {
$('#results').html(results);
}
});
});
var textlength = $('#query').val().length;
if (textlength <= 0) {
$('#query').focus();
} else {
$('#query').blur();
}
});
你可以做一個http://jsfiddle.net來演示嗎? –
如果URL是'http:// www.example.com /#type/query/someOtherString',您希望腳本將它視爲'http://www.example.com/#type/query/ '?或者你是否希望去掉'#type /'後面的所有內容,將它看作是:'http:// www.example.com /#type /'? –
難道你只是想爲URL做一個子字符串並檢測第一個'/'的位置? –