javascript - 仅使用 JavaScript(无 JQuery)检测特定“图例”元素何时在网页上可见
问题描述
我试图弄清楚如何使用 JavaScript 检测此特定元素何时出现在页面上?根据用户选择隐藏和显示多个图例元素。我无法触摸/更改网络应用程序的标记,我必须使用当前存在的内容。
<legend>Success! We've connected to your bank. Choose an account to fund your new Bank of the West account.</legend>
我正在玩/测试从另一个网站获得的以下代码,但它不起作用(我尝试了显示/无和可见性/隐藏)。我不明白函数中的图例变量如何返回图例元素中的实际文本,但是由于这一行,应该如何生成布尔值:
var foundAndVisible = legend && (window.getComputedStyle(legend).getPropertyValue("visibility") == "hidden");
function _dtmCheckFundingPage() {
var t = setInterval(function() {
var legend = document.evaluate("//legend[contains(., 'Choose an account to fund your new Bank of the West account.')]", document, null, XPathResult.ANY_TYPE, null).iterateNext();
console.log(" LEGEND HOLDS: ", legend);
//var foundAndVisible = legend && (window.getComputedStyle(legend).getPropertyValue("display") == "none"); // of course, replace display/none by visibility/hidden
var foundAndVisible = legend && (window.getComputedStyle(legend).getPropertyValue("visibility") == "hidden"); // of course, replace display/none by visibility/hidden
console.log(" B =============== IN FUNDING PAGE CHECK - foundAndVisible holds: " + foundAndVisible);
if (foundAndVisible) {
clearInterval(t);
console.log("==========found!");
}
}, 1000); // check every second
};
解决方案
我发现了这个问题。我不得不改变“foundAndVisible”的逻辑:
var foundAndVisible = legend && (window.getComputedStyle(legend).getPropertyValue("display") == "block") && (window.getComputedStyle(legend).getPropertyValue("visibility") == "visible");
推荐阅读
- r - Safari 浏览器上的 Shinyapps.io 无法连接到服务器“localhost”
- python-3.x - cv2.HoughCircles 需要超过 15 分钟才能运行
- javascript - 路由时AngularJs memoryLeak
- javascript - 如何使用firebase函数从文档中查询一个数字并将其全部汇总到firestore中的父文档中
- azure - 构建用于 PowerShell 查询的 Azure API 标头
- c++ - 如何:获取在 C++ 中的 NavigationView 中选择的项目
- matlab - 如何使用 matlab 选择微控制器来实现(在硬件中)脚本
- d3.js - D3js:“如何为不同的值附加不同的路径?”
- android - 如何连接到Android中的数据库?
- javascript - 使用 Firebase Cloud Firestore、Ionic Framework、Angular 获取服务器时间戳的最佳方法是什么?