我为我的网站下载了一个 livesearch JavaScript 插件,它可以正常工作,但我需要帮助对其进行一些修改。
这是代码:
$.ajax({url: $('base').attr('href') + 'index.php?route=product/search/ajax&keyword=' + keywords, dataType: 'json', type: 'GET', success: function(result) {
if( result.length > 0 ) {
var eList = document.createElement( 'ul' );
eList.id = 'livesearch_search_results';
var eListElem;
var eLink;
var eHref;
for( var i in result ) {
eListElem = document.createElement( 'li' );
eLink = document.createElement( 'a' );
if( result[i].img != null ) {
var eImg = document.createElement( 'span' );
eImg.className = "img_container";
var img = new Image();
img.src = result[i].img;
eImg.appendChild( img );
}
else {
eImg = document.createElement( 'span' );
eImg.innerHTML = ' ';
}
eDiv = document.createElement( 'div' );
eLink.appendChild( document.createTextNode(result[i].name) );
if( typeof(result[i].href) != 'undefined' ) {
eHref = result[i].href;
}
else {
eHref = $('base').attr('href') + 'index.php?route=product/product&product_id=' + result[i].product_id + '&keyword=' + keywords;
}
eLink.href = eHref;
eDiv.appendChild( eLink );
eDiv.innerHTML = eDiv.innerHTML + result[i].desc;
eListElem.appendChild( eImg );
eListElem.appendChild( eDiv );
eListElem.appendChild( document.createElement('br') );
eListElem.setAttribute( 'rel', eHref );
$(eListElem).bind('click', function(){
var gto = $(this).attr( 'rel' );
try {
if( gto != false && gto.length > 0 ) {
document.location = gto;
}
}
catch( e ) {}
});
eList.appendChild( eListElem );
}
if( $('#livesearch_search_results').length > 0 ) {
$('#livesearch_search_results').remove();
}
$('#search_menu').append(eList);
}
}});
输出是这样的(不带<ul>
):
<li rel="http://url">
<span class="img_container">
<img src="http://url.jp">
</span>
<div>
<a href="http://url">MONS 1.0 (CTM 2013)</a>
Description...</div>
<br>
</li>
有没有办法将输出修改为:
<li rel="http://url">
<a href="http://url"> <---- <a> tag here
<span class="img_container">
<img src="http://url.jp">
</span>
<span class="product-container"> <------ <span> tag here
<span class="title">MONS 1.0 (CTM 2013)</span> <------ <span> tag here, remove <a> tag
<span class="description">Description...</span> <------ <span> tag here
</div>
</a>
</li>
这超出了我的 JS 知识范围。
非常感谢