当我尝试附加 <li>
时属性到服务器列表,它不执行 forEach()
- 我试过了 console.log()
嵌套但它像 Servers
一样忽略它是空数组吗?
$(function() {
var Discord = {
Servers: [{
Name: 'Server 1',
Link: 'hidden for security'
}, {
Name: 'Server 2',
Link: 'hidden for security'
}]
};
$('#_discord_notify_btn').click(function() {
Discord.Servers.forEach(function($server) {
$.post($server.Link, {
content: $('#_discord_notify').val()
});
});
});
// this code is ignored??
Discord.Servers.forEach(function($server) {
$('_discord_servers ul').append("<li>" + $server.Name + "</li>");
});
});
textarea {
min-width: 447px;
min-height: 110px;
}
button {
border: 1px solid #000;
border-radius: 9px;
background: none;
padding: 10px;
}
button:hover {
background: #000;
color: #fff;
border: 1px solid #fff;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='_discord_servers'>
<b>Servers you're notifying</b>
<ul></ul>
</div>
<textarea id='_discord_notify'>Enter your notification...</textarea>
<button type='button' id='_discord_notify_btn'>
Send Notification
</button>
最佳答案
看起来您在第二个示例中缺少 ID 选择器的 #。
// this code is ignored??
Discord.Servers.forEach(function($server) {
$('#_discord_servers ul').append("<li>" + $server.Name + "</li>");
});
关于javascript - forEach 在 onClick 中工作正常但并非没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41967524/