【发布时间】:2018-12-21 18:48:45
【问题描述】:
我正在尝试使用 jquery 将每个锚标记中的所有文本聚合到一条消息中。如果它工作正常,我会看到包含以下内容的警报:
high_cases::pool_config::indy_pool_config_request_works_for_disabling_writing high_cases::pool_restart::indy_pool_restart_request_works_for_start_cancel_works
我目前在浏览器控制台中看到此错误:
未捕获的类型错误:减少没有初始值的空数组 在 Array.reduce() 在copyAllTestNamesToClipboard
我的 jquery 选择器不正确。正确的语法是什么?
<html>
<head>
<script>
function copyAllTestNamesToClipboard() {
var array = new Array();
$('div','a').each(function(){
array.push($(this).html());
});
var message = array.reduce(function(pre, next) {
return pre + '\n' + next;
});
alert(message);
}
</script>
</head>
<body>
<div class="panel-heading">
<h4 class="panel-title">
<button type="button" class="btn btn-link bnt-sm" onclick="copyAllTestNamesToClipboard()">(Copy)</button>
</h4>
</div>
<div id="error_group_collapse" class="panel-collapse collapse in">
<div class="panel-body">
<div class="panel-body-header">
<a data-toggle="collapse" href="#t189_error_group_collapse">high_cases::pool_config::indy_pool_config_request_works_for_disabling_writing</a>
</div>
<div class="panel panel-info">
<div id="t189_error_group_collapse" class="panel-collapse collapse in">
<!-- other stuff -->
</div>
</div>
<div class="panel-body-header">
<a data-toggle="collapse" href="#t192_error_group_collapse">high_cases::pool_restart::indy_pool_restart_request_works_for_start_cancel_works</a>
</div>
</div>
</div>
</body>
另外需要注意的是,我不想只选择 DOM 中的所有锚标记,因为可能存在我不想要的其他锚标记。我只想要带有id="error_group_collapse" 的div 中的锚标记。
谢谢帮助。
【问题讨论】:
-
你没有在reduce中设置pre的初始值...
标签: jquery