我正在尝试使用下拉按钮进行“过滤条件” 选择。
只要用户单击特定类型的商品(例如我的示例中的衣服),那么html只会显示符合条件的商品(仅衣服)。我可以设法选择所需的项并放入数组中,但是,当我尝试用新数组替换 div 时,输出为[object HTML Element]
。
有趣问题是如果我console.log
使用特定值的数组,(array[0])
它显示了我需要的html代码。
<div class="dropdown-menu" >
<a class="dropdown-item" onClick="filter('clothes')" >Clothes</a>
<a class="dropdown-item" onClick="filter('electronics')" >Electronics</a>
</div>
<div id="testSearch">
<section name="product">
<img value="clothes" src="./some.jpg" alt="Clothes 01">
<h5>Electronic</h5>
</section>
<section name="product">
<img value="electronics" src="./some2.jpg" alt="Electronics 01">
<h5>Electronic</h5>
</section>
</div>
<!-- My code has different sections with a variety of value/h5 -->
<script>
function filter(x) {
var arraySearch = [];
var input = document.getElementsByName('product');
for (i=0; i < input.length; i++) {
if(input[i].innerHTML.indexOf(x) > 0 ) {
arraySearch[i]= input[i];
result = result + arraySearch[i];
}
}
document.getElementById('testSearch').innerHTML = result;
<!-- my html display only [object HTMLElement], if more than a section matches, then it displays the [object HTMLElement] times the number of sections matched -->
}
</script>
我希望[object HTMLElement]的输出是符合条件的部分。
答案 0 :(得分:0)
我已经做到了!将数组传递到.outerHTML
var result.
这是工作代码:
<div class="dropdown-menu" >
<a class="dropdown-item" onClick="filter('clothes')" >Clothes</a>
<a class="dropdown-item" onClick="filter('electronics')" >Electronics</a>
</div>
<div id="testSearch">
<section name="product">
<img value="clothes" src="./some.jpg" alt="Clothes 01">
<h5>Electronic</h5>
</section>
<section name="product">
<img value="electronics" src="./some2.jpg" alt="Electronics 01">
<h5>Electronic</h5>
</section>
</div>
<!-- My code has different sections with a variety of value/h5 -->
<script>
function filter(x) {
var arraySearch = [];
var input = document.getElementsByName('product');
var result = " ";
for (i=0; i < input.length; i++) {
if(input[i].innerHTML.indexOf(x) > 0 ) {
arraySearch[i]= input[i];
result = result + arraySearch[i].outerHTML;
}
}
document.getElementById('testSearch').innerHTML = result;
}
</script>
就是这样。希望对别人有帮助。