javascript - jQuery 无法通过 ID 找到 IMG 组件

标签 javascript jquery

我有一个网页包含这样的代码:

<img class="img-qrcode" id="img_123.000.00.01" 
     src="http://localhost:7777/data/code_img\123.000.00.01.png" 
     alt="./data/code_img\123.000.00.01.png" style="display:none">

我想用 jQuery 来定位它。由于某种原因 jQuery 无法通过 ID 找到它,代码为:

$("#img_123.000.00.01")

添加的屏幕截图显示它返回一个空数组。

The img with this ID exists but jquery does not see it

为什么找不到ID为的元素?

最佳答案

使用attribute selector对于 id,您不必担心转义类选择器 (.)

let img = $("img[id='img_123.000.00.01']");
console.log(img.attr('src'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class="img-qrcode" id="img_123.000.00.01" 
     src="http://localhost:7777/data/code_img\123.000.00.01.png" 
     alt="./data/code_img\123.000.00.01.png" style="display:none">

关于javascript - jQuery 无法通过 ID 找到 IMG 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60257796/

相关文章:

javascript - 如何从第三方应用程序替换评论部分中的图像

javascript - 使用 jquery 连接时 Bootstrap 模式未加载

jquery - jqGrid。异步ajax调用

jquery - 原型(prototype)有 hash.inspect() 方法。 jQuery 世界中的等价物是什么?

javascript - JSON - 传递表单输入值作为 json 字符串出错

javascript - 更改 Three.js collada 对象的纹理和颜色

javascript - 在 NodeJS API 调用中推送和拉取数组中的元素

javascript - Javascript InfoVis 工具包的 'Hello, World' 是什么?

javascript - 使文本字段不可编辑,而不仅仅是只读

javascript - 处理背景色覆盖时边框底部不起作用