3 回答
TA贡献1816条经验 获得超4个赞
您可以尝试将相应的元素与“属性以选择器开头”[name^=“value”]和“属性以选择器结尾”[name$=“value”]
$('.myButton').click(function(){
$('div[id^=create]').hide(); //hide all
var id = $(this).attr('id'); //get the id of the clicked button
var end = id.slice(-2); //get last 2 character (LD/VC/FD) from id
$(`div[id$=${end}]`).show(); //match the div with id ends with the character and show
});
button {
padding: 20px;
margin: 0 5px 0 0;
border: 0 solid;
text-transform: uppercase;
letter-spacing: 1.5px;
color: #999999;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="button-container">
<button id="newLD" class="myButton">Logo</button>
<button id="newVC" class="myButton">Visiting Card</button>
<button id="newFD" class="myButton">Flyer Design</button>
</div>
<div id="createLD" style="display: none">
<p>Here is the lofo design div</p>
</div>
<div id="createVC" style="display: none">
<p>Here is the visitng card design div</p>
</div>
<div id="createFD" style="display: none">
<p>Here is the flyer design design div</p>
</div>
TA贡献1804条经验 获得超3个赞
$(".myButton").on("click", function() {
let id = $(this).attr("id");
if(id=='newLD'){
$('createLD').show()
$('createVC').hide()
$('createFD').hide()
}
else if(id=='newVC'){
$('createVC').show()
$('createLD').hide()
$('createFD').hide()
}
else if(id=='newFD'){
$('createFD').show()
$('createVC').hide()
$('createLD').hide()
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="button-container">
<button id="newLD" class="myButton">Logo</button>
<button id="newVC" class="myButton">Visiting Card</button>
<button id="newFD" class="myButton">Flyer Design</button>
</div>
<div id="createLD" style="display: none">
<p>Here is the lofo design div</p>
</div>
<div id="createVC" style="display: none">
<p>Here is the visitng card design div</p>
</div>
<div id="createFD" style="display: none">
<p>Here is the flyer design design div</p>
</div>
TA贡献1821条经验 获得超6个赞
您可以这样操作:获取单击的按钮的 id,隐藏 ID 以“create”开头的所有 div,并仅显示具有相应 id 的 div。
$(".myButton").on("click", function() {
let id = $(this).attr("id");
id = id.substr(3);
$("div[id^='create']").hide();
$("div[id='create" + id + "']").show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="button-container">
<button id="newLD" class="myButton">Logo</button>
<button id="newVC" class="myButton">Visiting Card</button>
<button id="newFD" class="myButton">Flyer Design</button>
</div>
<div id="createLD" style="display: none">
<p>Here is the lofo design div</p>
</div>
<div id="createVC" style="display: none">
<p>Here is the visitng card design div</p>
</div>
<div id="createFD" style="display: none">
<p>Here is the flyer design design div</p>
</div>
添加回答
举报