![](/img/trans.png)
[英]How to check if data-attribute does not contain a string with :contains in jQuery
[英]Count how many times a data-attribute or text string exists in a string and return the number for each one
我有一个PHP和JavaScript小应用程序,当该URL列表中的任何URL返回例如404或500状态代码时,cURL会请求一个URL列表并写入日志文件。
好的,我想做的是用JavaScript计算日志文件中每个URL的数量并返回数字。 我想返回这样的结构:
<ul>
<li>http://www.example.com (5)</li>
<li>http://anothersite.org (19)</li>
<li>http://www.more.com (16)</li>
</ul>
在日志文件中,我为每个URL条目赋予了一个数据属性,如下所示:
<p class="log-text">
<span data-url="www.examplezz.com">www.examplezz.com - 0 (Timeout)</span>
<span data-url="www.examplezz.com/othersite">www.examplezz.com/othersite - 0 (Timeout)</span>
<span data-url="www.idontexist.io">www.idontexist.io - 0 (Timeout)</span>
<span data-url="www.idontexist.io/othersite">www.idontexist.io/othersite - 0 (Timeout)</span>
</p>
<p class="log-text">
<span data-url="www.examplezz.com">www.examplezz.com - 0 (Timeout)</span>
<span data-url="www.examplezz.com/othersite">www.examplezz.com/othersite - 0 (Timeout)</span>
<span data-url="www.idontexist.io">www.idontexist.io - 0 (Timeout)</span>
<span data-url="www.idontexist.io/othersite">www.idontexist.io/othersite - 0 (Timeout)</span>
</p>
<p class="log-text">
<span data-url="www.examplezz.com">www.examplezz.com - 0 (Timeout)</span>
</p>
日志文件是通过ajax提取的,我将其作为字符串获取。 这是我尝试使用的带有“ for”循环的JS Bin:
JSBin附加信息:这是我的ajax请求的外观,不确定是否有帮助。
var overLoad = function (){ $.ajax({ type: 'POST', url: 'includes/logfiles/'+logfilename+'.php', data: $('.log-text'), statusCode: { 404: function() { $("<p class='intro log-text eventNum' style='margin-bottom:0;'>Is your internet connection intact?</p>").appendTo(".overview-popup"); } } }).done(function (data) { // here I want to count the urls in the 'data' string }).fail(function (jqXHR, statusCode) { $("<p class='intro log-text eventNum' style='margin-bottom:0;'>No offline events!</p>").appendTo(".overview-popup"); }); };
当您掌握了DOM时,为什么还要尝试进行字符串操作?
var div = document.createElement('div');
div.innerHTML = data;
var elements = div.querySelectorAll('[data-url]');
var urls = {};
for(var i = 0; i < elements.length; i++) {
var url = elements[i].getAttribute('data-url');
if(!(url in urls)) {
urls[url] = 0;
}
urls[url]++;
}
console.log(urls);
http://jsbin.com/adEBiCU/3/edit
作为练习,我将按主机名(而不只是URL)分组。 提示:使用DOM(双重提示: <a>
具有host
属性)。
编辑:我想念您已经在使用jQuery。
var data ='<p class="log-title">not available. <span class="stamp"><span >Pinged: </span>Wednesday 1st of January 2014 03:57:03 PM</span></p><p class="log-text"><span data-url="www.examplezz.com">www.examplezz.com - 0 (Timeout)</span><br /><span data-url="www.examplezz.com/othersite">www.examplezz.com/othersite - 0 (Timeout)</span><br /><span data-url="www.idontexist.io">www.idontexist.io - 0 (Timeout)</span><br /><span data-url="www.idontexist.io/othersite">www.idontexist.io/othersite - 0 (Timeout)</span><br /></p>';
var urls = $(data).find('[data-url]').map(function() {
var url = $(this).attr('data-url');
return $('<a>', {href: 'http://' + url})[0].host;
}).get().reduce(function(urls, b) {
if(!(b in urls)) {
urls[b] = 0;
}
urls[b]++;
return urls;
}, {});
console.log(urls);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.