javascript - 通过动态生成的标签获取图片源

标签 javascript jquery

我在该标签内有 span 标签,新的图像标签是动态生成的,但我需要获取该标签的图像 src 是否可能

<span id ="Imagespan">
    <image scr="some.png" alt="some" />
<span>  

最佳答案

你的意思是img而不是image,是的,你可以,试试:

$('#Imagespan').find('img').attr('src');
//or also
$('#Imagespan').find('img').prop('src');

或者使用直接选择器,例如:

$('#Imagespan img').prop('src');

注意:无论元素是否动态创建,获取“属性/属性”的方法都是相同的。

.attr() changes attributes for that HTML tag.

.prop() changes properties for that HTML tag as per the DOM tree.

As the example in this link suggests. An input field can have the attribute "value". This will equal the default value you entered. If the user changes the value in the input field, the property "value" changes in the DOM Tree, but the original attribute is left remaining.

<强> Source

关于javascript - 通过动态生成的标签获取图片源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41023718/

相关文章:

javascript - Ajax 和 Jquery 中的 if else 条件不起作用

c# - mvc post action json 未解析为参数

javascript - 单击即可更改范围文本

javascript - 从浏览器打开文件时出现奇怪的 Javascript/vbscript 行为

javascript - 插入标签(所见即所得的 JavaScript 编辑器)

javascript - Durandal 如何附加 View 并在准备好之前不显示它?

javascript - 如何跟踪在 Javascript 中创建了多少个数组?

javascript - jquery 在其他事件处理程序中调用事件处理程序函数

jquery - 结合 jQuery 可拖动、可放置和可排序

jquery - 使用Django的JSONResponseMixin响应AJAX请求