道歉,但无法找到此问题的答案。我在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
尝试从事件处理函数返回值没有任何意义。它应该返回到哪里?不是你调用事件处理程序,而是事件发生时的浏览器。 –