我有一個input
,應該添加一個clicked
類的另一個元素的ID爲#zip
點擊。下面是代碼:如何添加一個類到一個元素,當我點擊與jquery不同的元素
$('#billing_zip').click(function() {
$('#zip').addClass('clicked');
});
#zip {
color: #444;
font-style: italic;
position: absolute;
top: 8px;
left: 35px
}
.clicked {
display: none;
}
<div class="chkField">
<label for="billing_zip">Zip</label>
<input type="text" onchange="clearContent(this);check_address('billing');" class="txtBoxStyle hasPlaceholder" tabindex="10" size="10" value="" id="billing_zip" maxlength="15" name="billing_zip">
<!--START: req_billing_zip-->
<img width="12" height="12" alt="" src="assets/templates/common/images/error2.gif">
<!--END: req_billing_zip-->
<div class="clear"></div>
<div id="zip">zip</div>
</div>
我不知道爲什麼上面的jQuery不工作。
至於我可以看到你的代碼工作得精細。 「點擊」類正在得到應用。 http://jsfiddle.net/8L5qdeu7/ – 2014-12-05 04:30:46
不要忘記把你的jQuery代碼包裝在'$(document).ready(function(){//你的jquery代碼//});' – 2014-12-05 04:31:27
add id ='billing_zip '在標籤。您將billing_zip聲明爲自定義屬性(for)。但是你選擇它作爲id。 – zanhtet 2014-12-05 04:32:19