我想要做的是允許用戶在下面的搜索框中(在HTML中)基於藝術家的名字尋找藝術家,然後向用戶呈現藝術家的名字和鏈接到如果用戶的搜索與數組中的藝術家匹配,則使用不同的HTML頁面。創建一個搜索框
麻煩的是,當我點擊「搜索」頁面會回到原來的頁面。我不確定斷開連接的位置。我不確定我是否正確地呼叫ID;如果身份證在正確的地方;如果該操作在U.addEvent函數中是正確的;或者如果搜索功能寫入正確。
任何想法?
RD
<body>
<div class="imgs"> <a target="_blank" href=""><img src="img/04.png" alt="img" /></a></div>
</div>
<div class="form">
<form method="post" action="handler.php">
<input name="textfield" id="searchBox" type="text" class="colortext"/>
</form>
</div>
</div>
<script>
var fakeDatabase = [];
var foxyShazam = {
id: 'foxyShazam_FS',
title: 'Foxy Shazam',
artist: 'Foxy Shazam',
price: '$14.99',
releaseDate: new Date(1968, 10, 22),
quantity: 50,
link: "albums/foxyShazam.html",]
};
var foxyShazam2 = {
id: 'foxyShazam_FS_2',
title: 'Foxy Shazam 2',
artist: 'Foxy Shazam 2',
price: '$14.99',
releaseDate: new Date(1968, 10, 22),
quantity: 50,
link: "albums/foxyShazam.html"]
};
var thriller = {
id: 'thriller_MJ',
title: 'Thriller',
artist: 'Michael Jackson',
price: '$12.99',
releaseDate: new Date(1982, 10, 30),
quantity: 35,
link: "albums/thriller.html"
};
var thriller2 = {
id: 'thriller_MJ_2',
title: 'Thriller 2',
artist: 'Michael Jackson 2',
price: '$12.99',
releaseDate: new Date(1982, 10, 30),
quantity: 35,
link: "albums/thriller.html"]
};
fakeDatabase.push(foxyShazam, foxyShazam2, thriller, thriller2);
//function displayAlbum() {
// 'use strict';
// for (var i=0; i < fakeDatabase.length; i++) {
// alert(fakeDatabase[i].title);}
//};
//displayAlbum(fakeDatabase);
var U = {
$: function (id) {
'use strict';
if (typeof id === 'string') {
return document.getElementById(id);
}
},
setText: function(id, message) {
'use strict';
if ((typeof id == 'string')
&& (typeof message == 'string')) {
// Get a reference to the element:
var output = this.$(id);
if (!output) return false;
// Set the text
if (output.textContent !== undefined) {
output.textContent = message;
} else {
output.innerText = message;
}
return true;
} // End of main IF.
},
addEvent: function(obj, type, fn){
'use strict';
if (obj && obj.addEventListener) {
obj.addEventListener(type, fn, false);
} else if (obj && obj.attachEvent) {
obj.attachEvent('on' + type, fn);
}
},
removeEvent: function(obj, type, fn) {
'use strict';
if (obj && obj.removeEventListner) {
obj.removeEventListner(type, fn, false);
}
else if (obj && obj.removeEvent) {
obj.removeEvent('on' + type, fn);
}
}
};
function search() {
'use strict';
var newSearch = U.$('searchBox');
if (typeof newSearch == 'string') {
if (!newSearch)
{return false;};
if (newSearch.textContent !== undefined) {
for (var i=0; i < fakeDatabase.length; i++) {
if (i == newSearch) {
var options = document.getElementById('options');
(options.innerText = ((fakeDatabase[i].artist + '\n') + (fakeDatabase[i].link));
return true;
}
}
}
}
};
U.addEvent(U.$('searchBox'), 'click', search());
</script>
</body>
敢肯定這應該是'U.addEvent( ...,...,搜索)','search'後面沒有括號 – lordvlad