所以我有一個問題我的HTML有一些div旁邊的價格之一divred alredy有在html中的選擇以及我必須做的是,在JS中我必須取消選擇所選內容,只是與1選中但當我選擇其中一個不會改變。如何在div之間切換並取消選擇所選的div?
這裏是我的html
<div id="package-container">
<div class="package-center">
<p class="green-titles"><b>AVAILABLE PACKAGES:</b></p>
</div>
<div class="coverage-types">
<div class="coverage-title-container">
<p><b>BASIC COVERAGE</b></p>
</div>
<p class="coverage-titles"><b>INCLUDES:</b></p>
<ul class="coverage-list">
<li >
<p>CDW</p>
</li>
</ul>
<div class="firstPackage-confirmation">
<div class="selected-package">
<img src="./assets/packages/selected.png" alt="">
</div>
<p class="coverage-price"><b>$30.98 USD</b></p>
</div>
</div>
<div class="coverage-types">
<div class="coverage-title-container">
<p><b>BASIC COVERAGE+</b></p>
</div>
<p class="coverage-titles"><b>INCLUDES:</b></p>
<ul class="coverage-list">
<li > <p>CDW</p> </li>
<li > <p class="extra-space">+1 DRIVER</p> </li>
</ul>
<div class="package-confirmation">
<div class="unselected-package">
</div>
<p class="selectedCoverage-price"><b>$30.98 USD</b></p>
</div>
</div>
<div class="coverage-types">
<div class="coverage-title-container">
<p><b>MID COVERAGE</b></p>
</div>
<p class="coverage-titles"><b>INCLUDES:</b></p>
<ul class="coverage-list">
<li > <p>CDW</p> </li>
<li > <p class="extra-space">+1 DRIVER</p> </li>
<li > <p>SURCHARGES</p> </li>
<li > <p>EXT. PROTECTION</p> </li>
</ul>
<div class="package-confirmation">
<div class="unselected-package">
</div>
<p class="selectedCoverage-price"><b>$30.98 USD</b></p>
</div>
</div>
<div class="coverage-types">
<div class="coverage-title-container">
<p><b>MID COVERAGE+ </b></p>
</div>
<p class="coverage-titles"><b>INCLUDES:</b></p>
<ul class="coverage-list">
<li > <p>CDW</p> </li>
<li > <p class="extra-space">+1 DRIVER</p> </li>
<li > <p>SURCHARGES</p> </li>
<li > <p>EXT. PROTECTION</p> </li>
<li > <p>FIRST TANK</p> </li>
</ul>
<div class="package-confirmation">
<div class="unselected-package">
</div>
<p class="selectedCoverage-price"><b>$30.98 USD</b></p>
</div>
</div>
<div class="coverage-types">
<div class="coverage-title-container">
<p><b>FULL COVERAGE</b></p>
</div>
<p class="coverage-titles"><b>INCLUDES:</b></p>
<ul class="coverage-list">
<li > <p>CDW</p> </li>
<li > <p class="extra-space">+1 DRIVER</p> </li>
<li > <p>SURCHARGES</p> </li>
<li > <p>EXT. PROTECTION</p> </li>
<li > <p>FIRST TANK</p> </li>
</ul>
<div class="package-confirmation">
<div class="unselected-package">
</div>
<p class="selectedCoverage-price"><b>$30.98 USD</b></p>
</div>
</div>
<div class="coverage-types">
<div class="coverage-title-container">
<p><b>FULL COVERAGE+</b></p>
</div>
<p class="coverage-titles"><b>INCLUDES:</b></p>
<ul class="coverage-list">
<li > <p>CDW</p> </li>
<li > <p class="extra-space">+1 DRIVER</p> </li>
<li > <p>SURCHARGES</p> </li>
<li > <p>EXT. PROTECTION</p> </li>
<li > <p>FIRST TANK</p> </li>
<li > <p class="extra-space">GPS</p> </li>
</ul>
<div class="package-confirmation">
<div class="unselected-package">
</div>
<p class="selectedCoverage-price"><b>$30.98 USD</b></p>
</div>
</div>
</div>
,這裏是我的js
$(document).ready(function(){
var imagen = $('<img />');
imagen.attr('src', './assets/packages/selected.png');
var secimagen = $('<img />');
secimagen.attr('src', './assets/payment/checkmark.png');
$(".unselected-package").on("click",function(){
$(this).addClass("selected-package");
$(this).removeClass("unselected-package");
})
$(".selected-package").on("click",function(){
$(".selected-packages").addClass("unselected-packages");
$(".selected-packages").removeClass("selected-packages");
});
});
不是有兩個'click'處理程序,是不是很容易,只需有一個設置'this'有'選擇包'類和所有其他元素有'selected-package'被刪除? –
你正在引用複雜的類'packages',看來你需要引用單個'package'代替。 – UncaughtTypeError