javascript - If else 取决于脚本类型
问题描述
所以我正在研究一个小片段来确定何时注入 HTML,并且我很难正确定位正确的条件。
所以下面,我将详细介绍我正在尝试做的事情:
我<script>
在页脚中嵌入了以下内容,其 ID 根据插件将“text/plain”更改为“text/javascript”:
<script type="text/plain" id="google-maps-api-script" class="optanon-category-C0004" src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"></script>
在 js 中,我在控制台中使用以下命令记录元素 ID:
const script = $('#google-maps-api-script');
console.log(script);
这给了我一个带有 [0] 索引的 S.fn.init 返回:
现在在属性内部,我希望能够根据脚本类型执行 If/else 条件。所以属性 -> [0] 或类型 -> nodeValue/value 表明:
我怎样才能正确地做出一个条件说:
If (#google-maps-api-script type is 'text/plain') {
// Do something
} else or if it's 'text/javascript')) {
// Do something else
}
所有帮助将不胜感激!
解决方案
用于.attr('type')
获取它:
if ($("#google-maps-api-script").attr("type") == "text/plain")) {
// do something
} else {
// do something else
}
推荐阅读
- javascript - 提交重力表单后,在同一页面上显示内容
- php - 表单提交跳过/忽略AJAX功能
- reactjs - electron + react + webpack dev 中间件 + hmr - 刷新整个页面而不是热重新加载
- python - 如何将数字四舍五入到指定的上限或下限?
- python-3.x - 如何生成所有可能的数字组合以有效地检查费马大定理
- google-colaboratory - 在 google colab 中挂载多个驱动器
- angular - Rxjs - 以可观察值作为值重新映射对象
- birt - 在 BIRT 中使用 POJO 数据源嵌套表
- mysql - MYSQL 使用连续数值 IF NULL 更新列 - 继续序列
- loops - 弹性搜索循环两个索引的值