道歉,但無法找到此問題的答案。我在HTML上找到項目並將其推送到一個對象,但我無法弄清楚我的生活,爲什麼這個函數不返回對象?我可以在沒有問題的情況下進行安裝。嘗試從函數返回對象
function getItem(obj){
var itemsObj;
$('.info').on('click', function(){
itemsObj = {};
$this = $(this);
itemsObj.gameImg = $this.parent().parent().find('img').attr('src');
itemsObj.gameTitle = $this.parent().parent().find('p').text();
itemsObj.gameInfo = $this.parent().parent().find('h2').text();
// console.log(itemsObj);
return itemsObj;
});
}
//getItem();
console.log(getItem());
<div class="col-lg-3 game">
<div class="view view-first">
<img src="img/deathstranding.jpg"/>
<div class="mask">
<h2>Death Stranding</h2>
<p>Death Stranding is an upcoming action video game developed by Kojima Productions and published by Sony Interactive Entertainment for PlayStation 4. It is the first game by game director Hideo Kojima and his company following the 2015 disbandment of Kojima Productions as a subsidiary of Konami and subsequent reformation as an independent studio.</p>
i<a href="#" class="info">♥</a>
</div>
</div>
你的外部函數沒有return語句。 – david25272
此外itemsObj只會在用戶單擊.info元素後纔會填充,而不是在調用getItem函數時調用 – Frankusky
嘗試從事件處理函數返回值沒有任何意義。它應該返回到哪裏?不是你調用事件處理程序,而是事件發生時的瀏覽器。 –