javascript - 编辑链接的innerText禁用点击链接
问题描述
在 9 秒不活动后,我的函数将字母“o”替换为一朵花。
当我在链接上使用它时,例如<a href="google.com">Click on me</a>
无法单击链接。
这只发生在 Safari 中。我正在使用版本 11.1.2。它适用于 Chrome 和 Firefox。
Safari 有什么不同的做法导致了这个问题?
我发现 wwindow.onmousedown
事件监听器是罪魁祸首。在 Safari 中单击链接的能力很混乱。
var elements = document.querySelectorAll('.flowerText');
var functionIsRunning = false;
function elementSelector(x, y) {
var strNewNew = elements[x].innerText;
var oneMoreFlower
function changeToFlowers() {
if (functionIsRunning) {
return;
}
oneMoreFlower = strNewNew.replace(/o/, "✿");
elements[x].innerText = oneMoreFlower;
strNewNew = oneMoreFlower;
return strNewNew;
}
var newVariable = (strNewNew.match(/o/g) || []).length; //logs 3
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min);
}
for (var i = 0; i < newVariable; i++) {
var int = randomIntFromInterval(0, 1500);
setTimeout(changeToFlowers, int)
}
}
var numOfElements = document.querySelectorAll('.flowerText').length;
function changeToOs() {
if (!functionIsRunning) {
functionIsRunning = true;
elements.forEach(function(company) {
var textContentVar = company.innerText;
company.innerText = textContentVar.replace(/✿/g, "o");
return;
});
functionIsRunning = false;
}
}
function idleLogout() {
var t;
window.onload = resetTimer;
window.onmousemove = resetTimer;
window.onmousedown = resetTimer; // catches touchscreen presses as well
window.ontouchstart = resetTimer; // catches touchscreen swipes as well
window.onclick = resetTimer; // catches touchpad clicks as well
window.onkeypress = resetTimer;
window.addEventListener('scroll', resetTimer, true); // improved; see comments
function yourFunction() {
console.log("inactive");
for (var i = 0; i < numOfElements; i++) {
elementSelector([i]);
}
changeToOs();
}
function resetTimer() {
console.log("timer has been reset");
clearTimeout(t);
t = setTimeout(yourFunction, 9000); // time is in milliseconds
changeToOs();
}
}
idleLogout();
console.log(functionIsRunning)
<a href="google.com" class="flowerText">GooGLE 1</a><br>
<a href="google.com" class="flowerText">GoOGLE 2</a><br>
<a href="google.com" class="flowerText">GOoGLE 3</a><br>
<a href="google.com" class="flowerText">GOOGLE 4</a><br>
<a href="google.com" class="flowerText">GooGLE 5</a>
解决方案
推荐阅读
- android - 我们如何在 Android Q 中访问扩展文件?
- javascript - 从 WP Query 获取数据并放入 javascript 数组
- reactjs - 如何将包含子文件夹中的 create-react-app 的节点项目推送到 GitHub?
- raspberry-pi - 我无法在 Raspberry Pi 的 thingsboard 中创建仪表板
- javascript - 每当 redux 形式的有效性发生变化时触发事件
- mysql - 如何编写一个查询,给出没有蓝房子的每个人的名字?
- json - How to fix the "no value associated" error with CodingKeys?
- oauth-2.0 - Google API 服务不接受生成的 JSON Web Token
- c++ - 将函数名称传递给事件总线系统中的事件类
- c - 无法在代码块中调试 C 程序