Javascript 当触发DOMContentLoaded事件时,如何不调用函数restClient(url)?
Javascript 当触发DOMContentLoaded事件时,如何不调用函数restClient(url)?,javascript,jquery,html,google-chrome-extension,Javascript,Jquery,Html,Google Chrome Extension,restClient()是在HTML页面上进一步写入的函数,现在的问题是第一次调用domcontentnloadrestClient并将其写入HTML页面,然后执行下一行:document.querySelector('button')。addEventListener('click',getWeatherByLocation); 当有人触发按钮时,最终从getWeatherByLocation函数再次调用函数restClient, 但是当有人再次触发按钮时,第4行是如何再次执行的 我想在再次执
restClient()
是在HTML
页面上进一步写入的函数,现在的问题是第一次调用domcontentnload
restClient并将其写入HTML
页面,然后执行下一行:document.querySelector('button')。addEventListener('click',getWeatherByLocation);
当有人触发按钮时,最终从
getWeatherByLocation
函数再次调用函数restClient
,
但是当有人再次触发按钮时,第4行是如何再次执行的
我想在再次执行第4行之前,如果我没有错的话,必须执行第2、3行
当页面第一次加载时,DOMContentLoaded只会触发一次 如果希望在每次运行
getWeatherByLocation
时执行该其他代码,则应将其移动到getWeatherByLocation
函数的末尾
请参阅文档以了解DOMContentLoaded
:
如果
getWeatherByLocation
无法重新加载DOM,为什么会再次调用第2行和第3行?或者我没有理解您的问题?编辑混乱使问题几乎无法理解。此外,domcontentloaded通常不是内容脚本的正确位置。根据扩展加载模式,它永远无法获得c好的。哦,你说得100%对!对不起,我完全错过了chrome扩展标签!
document.addEventListener('DOMContentLoaded', function() {
var url = "http://api.openweathermap.org/data/2.5/weather?q=" + "ranchi" + "&units=metric";
restClient(url);
document.querySelector('button').addEventListener('click', getWeatherByLocation);
});
function getWeatherByLocation() {`......do some work here..
var url=.....;
restClient(url);
}`
function restClient(url) {....//do some work here...
document.getElementById("data").innerHTML="Something to write"..;
}