问题描述
我有以下javascript代码将上传者名称放在上传日期之前,并在youtube搜索结果中查看计数。
I have the following javascript code to put the uploader name before the upload date and view count in youtube search results.
function hasClass(element, cls) {
return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}
var elems = document.getElementsByClassName('yt-lockup-meta-info');
var elemss = document.getElementsByClassName('yt-uix-sessionlink g-hovercard spf-link');
var elemsss = 1;
var elemssss = 0;
var myElem = document.getElementById('sb-button-notify');
if (myElem == null) elemsss = 0; // these two lines are here to skip the additional element
// that is only there if you are logged in
for (var i = 0; i < elems.length; ++i) {
if (hasClass(elemss[i+elemsss],'yt-uix-tile-link'))
{elemssss=elemssss+1;elemsss=elemsss+3;alert('damn');}
elems[i+elemssss].insertBefore(elemss[i+elemsss],elems[i+elemssss].firstChild);}
如何检查多类元素的整个类属性,而不是hasClass函数?这不起作用:
Instead of the hasClass function, how can I check the whole class attribute of a multiple class element? This didn't work:
element.className=='yt-uix-sessionlink yt-uix-tile-link
yt-ui-ellipsis yt-ui-ellipsis-2 g-hovercard spf-link'
我只需要检查类,因此代码可以跳过具有特定类的元素。更好的解决方案是document.getElementsByClassName的替代方案,它只获取具有完全给定类的元素,并忽略具有MORE类的元素。
I just need to check the class, so the code can skip elements with a specific class. An even better solution would be an alternative to document.getElementsByClassName, which would only get elements with exactly the given classes, and ignore ones that have MORE classes.
推荐答案
element.classList
它会返回元素
上存在的所有类的数组,如 [site-icon,favicon,favicon-stackoverflow]
,那么通过使用普通的javascript,你可以实现自己的hasClass功能。
It would return you the array of all the classes present on the elementlike ["site-icon", "favicon", "favicon-stackoverflow"]
, so then by using normal javascript you can implement hasClass functionality of your own.
所以你的hasClass函数变为
So your hasClass function becomes
function hasClass(element, cls){
return element.classList.contains(cls);
}
这篇关于如何在没有jquery的情况下在javascript中检查多个类元素的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!