addEventListener只在页面刷新工作? [英] addEventListener only working at page refresh?
问题描述
我正在修改两个下拉列表的外观。这里没有问题。一切都很好。唯一的问题是 addEventListener
方法仅适用于页面刷新。
如何在页面加载时使这段代码无需点击刷新按钮?
window.addEventListener('load',function()
{
var CityCount = this.character.citynum;
var PosX = parseInt(CityCount)*(-15) ;
var MyHeight = parseInt(CityCount)* 15 - 15;
var MyStyle ='div#citylist {width:150px!important; margin-top:'+ PosX +'px!important; position:绝对!重要;身高:'+ MyHeight +'px!重要;溢出:自动!重要;填充左:1px!重要}';
addGlobalStyle(MyStyle);
addGlobalStyle('div#my_city_list {width:150px!important; margin-top:50px!important;}');
},false)
<您没有列出目标网页,但它可能使用AJAX设置和/或更改该全局变量。
另外,如果脚本丢失,问题代码将会中断 @grant none
状态,或者如果您尝试在任何浏览器上使用它,但Firefox。 (除非脚本使用 Injection - 这是我们无法从问题中看出的)。
为了解决AJAX问题, setInterval()
中的变量。
为了使代码更健壮,可以使用 unsafeWindow
或脚本注入。有关更多信息,请参见从Greasemonkey访问变量... 。
把它放在一起,这应该工作。
addEventListener()
不是必需的: $ b
var globScope = unsafeWindow ||窗口;
var cityCountTimer = setInterval(styleTheCityList,333);
函数styleTheCityList(){
this.lastCityCount = this.lastCityCount || 0; //这个func的静态变量
if(
typeof globScope.character!=undefined
&& typeof globScope.character.citynum!=undefined
){
var CityCount = parseInt(globScope.character.citynum,10);
if(CityCount!= this.lastCityCount){
var PosX = CityCount *(-15);
var MyHeight = CityCount * 15 - 15;
var MyStyle ='div#citylist {width:150px!important; margin-top:'
+ PosX
+'px!important;位置:绝对!重要;身高:'
+ MyHeight
+'px!important;溢出:auto!important; padding-left:1px!important}'
;
addGlobalStyle(MyStyle);
addGlobalStyle('div#my_city_list {width:150px!important; margin-top:50px!important;}');
this.lastCityCount = CityCount;
}
}
}
I´m modifying the appearance of two drop down lists. No problems here. Everything works great. The only issue is that the addEventListener
method only works at page refresh.
How do I make this code work at page load without hitting the refresh button?
window.addEventListener('load', function ()
{
var CityCount = this.character.citynum ;
var PosX = parseInt(CityCount) * (-15);
var MyHeight = parseInt(CityCount) * 15 - 15;
var MyStyle='div#citylist {width: 150px !important; margin-top: ' + PosX + 'px !important; position: absolute !important; height: ' + MyHeight + 'px !important; overflow: auto !important; padding-left: 1px !important}';
addGlobalStyle(MyStyle);
addGlobalStyle('div#my_city_list {width: 150px !important; margin-top: 50px !important;}');
}, false)
You didn't list the target page, but it probably uses AJAX to set and/or change that global variable.
In addition, the question-code will break if the script loses its @grant none
status, or if you try to use it on any browser but Firefox. (Unless the script uses Injection -- which we can't tell from the question.)
To get around the AJAX problem, poll for the variable inside a setInterval()
.
To make the code more robust, use unsafeWindow
or Script Injection. See "Accessing Variables from Greasemonkey..." for more information.
Putting it all together, this should work. addEventListener()
is not needed:
var globScope = unsafeWindow || window;
var cityCountTimer = setInterval (styleTheCityList, 333);
function styleTheCityList () {
this.lastCityCount = this.lastCityCount || 0; // Static var for this func
if (
typeof globScope.character != "undefined"
&& typeof globScope.character.citynum != "undefined"
) {
var CityCount = parseInt (globScope.character.citynum, 10);
if (CityCount != this.lastCityCount) {
var PosX = CityCount * (-15);
var MyHeight = CityCount * 15 - 15;
var MyStyle = 'div#citylist {width: 150px !important; margin-top: '
+ PosX
+ 'px !important; position: absolute !important; height: '
+ MyHeight
+ 'px !important; overflow: auto !important; padding-left: 1px !important}'
;
addGlobalStyle (MyStyle);
addGlobalStyle ('div#my_city_list {width: 150px !important; margin-top: 50px !important;}');
this.lastCityCount = CityCount;
}
}
}
这篇关于addEventListener只在页面刷新工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!