当我点击应该出现的电池时,其他物品应该消失。
function serviceid(id){
var searchid=id;
alert(searchid);
i f (searchid!=="")
{
if(document.getElementById("myText").value!=="")
{
document.getElementById("myText").value+=","+searchid;
var x = document.getElementById('myText');
if (x.style.display === 'none')
{
x.style.display = 'block';
}
else
{
x.style.display = 'none';
}
}
else{
document.getElementById("myText").value+=searchid;
}
} //document.getElementById("myText").value+=','+searchid;
else{
document.getElementById("myText").value+=searchid;
alert(document.getElementById("myText").value);
}
}
答案 0 :(得分:0)
尝试这个
$(document).ready(function() {
$('ol li').click(function() {
$("ol li").not(this).remove();
});
});
ol li {
cursor: pointer;
padding: 3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ol>
<li>Battery</li>
<li>Car Battery Replacement</li>
<li>Service Battery/cables</li>
<li>Serpentine/Drive Belt Replacement</li>
<li>Service Battery/cables</li>
<li>xxx</li>
</ol>
我觉得它有用
答案 1 :(得分:0)
试试这个:
<html>
<head>
</head>
<body>
<ol>
<li>a</li>
<li>b</li>
<li>c</li>
</ol>
<script src="jquery.js"></script>
<script>
$( "li" ).click(function() {
$("li").hide();
$( this ).show();
});
</script>
</body>
答案 2 :(得分:0)
这是一个javascript解决方案,当你点击一个li元素时,它会显示它并隐藏其余的li元素。
var li = document.getElementsByTagName('li');
Array.from(li).forEach( function(element) {
element.addEventListener("click", function(event){
Array.from(li).forEach(function(element){
if(element === event.target){
element.style.display = 'list-item';
} else {
element.style.display = 'none';
}
});
});
});
<ul>
<li>Battery</li>
<li>Car Battery Replacement</li>
<li>Service Battery/cables</li>
<li>Serpentine/Drive Belt Replacement</li>
<li>Service Battery/cables</li>
<li>xxx</li>
</ul>
答案 3 :(得分:0)
使用index()
和eq()
的最简单的单行解决方案:
$('ol li').on('click', function() {
$('ol li').hide().eq($(this).index()).show();
});
&#13;
ol li {
cursor: pointer;
padding: 3px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ol>
<li>Battery</li>
<li>Car Battery Replacement</li>
<li>Service Battery/cables</li>
<li>Serpentine/Drive Belt Replacement</li>
<li>Service Battery/cables</li>
<li>xxx</li>
</ol>
&#13;