我正在尝试确定div是否具有滚动条,然后删除该div。
不知道我在这里做错了什么,但我无法检测到它。
Jsfiddle:https://jsfiddle.net/cyj2sb0g
(function($) {
$.fn.hasHorizontalScrollBar = function() {
return this.get(0) ? this.get(0).scrollWidth > this.innerWidth() : false;
}
})(jQuery);
if($(".ls-boxscore").hasHorizontalScrollBar()){
$(this).remove();
}
答案 0 :(得分:1)
您的hasHorizontalScrollBar
功能正常。
在if
块中,this
并不指向该div,而是指向window
对象,将其更改为:
if($(".ls-boxscore").hasHorizontalScrollBar()){
$(".ls-boxscore").remove();
}
答案 1 :(得分:1)
您可以尝试使用此代码。
您也可以通过向元素添加ID
来实现。使用id
,则无需进入element[0]
(function($) {
$.fn.hasScrollBar = function() {
var attrClass = $(this).attr("class");
var elmnt = document.getElementsByClassName(attrClass);
return elmnt[0].scrollWidth > this.width();
}
})(jQuery);
$(function(){
var scrollBarFound = $('.ls-boxscore').hasScrollBar();
if(scrollBarFound) {
console.log('scrollbarfound');
$('.ls-boxscore').remove();
}
});