1
我試圖通過集合的類名來獲得給定集合中的任何元素。
我有一個(工作)的功能,上載其追加新要素的集合(忽略了第一位,追加爲末):
function sortMaps(returnString) {
returnArray = returnString.split(",");
imageCount = parseInt(returnArray[0]);
for (i = 0; i < imageCount; ++i) {
MapObject = {
id: returnArray[i+1],
url: returnArray[i+imageCount+1],
name: returnArray[i+(imageCount*2)+1],
coordString: returnArray[i+(imageCount*3)+1],
width: returnArray[i+(imageCount*4)+1],
height: returnArray[i+(imageCount*5)+1]
};
mapArray[i] = MapObject;
$("#mapList").append("<li id=\"" + i + "dropdownMap\" class=\"dropdownMap\">" + mapArray[i].name + "</li>");
}
}
而另一個函數,就像一個測試儀,打印出的id和class取其元素我點擊:
$(document).click(function (event) {
console.log(event.target.id);
console.log($(event.target.id).attr("class"));
});
的HTML到我追加的東西看起來是這樣的:
<div id="mapperMenu">
<div class="mapmakerDropdown">
<button>Maps</button>
<div class="mapmakerDropdownOptions">
<ul id="mapList">APPENDED ELEMENTS APPEAR HERE</ul>
</div>
</div>
<form action="returnMapmaker.php" method="post">
<input type="submit" class="button" value="Save">
<form>
</div>
當我點擊一個附加列表元素時,ID出現並且是正確的,但該類未定義(儘管它出現在Firefox檢查器中的HTML中)。所以在這裏我是因爲我不確定爲什麼會發生這種情況。
任何幫助表示讚賞!如果您需要更多信息,請告知我:)
使用 '$(event.target).attr( 「類」)'。如果你真的想使用id,可以使用'$('#'+ event.target.id).attr(「class」)' – Reeno
@Reeno'this'是'document'! –
是的,你是對的,我改變了。 – Reeno