首页 > 解决方案 > 在js jquery中获取孩子的src

问题描述

我需要比较 2 个元素,当它们是文本时,这是可行的:

if (first.children().text() != second.children().text())

但是,当它们是图像时,以下两个都不起作用,有什么想法吗?

if (first.children().getAttribute('src') != second.children().getAttribute('src'))
if (first.children().src() != second.children().src())

标签: javascriptjquery

解决方案


getAttribute()不是 jQuery 函数,它是可以在 DOM 元素上调用的原生 JS 函数。

您正在寻找 jQuery 的attr()功能。


推荐阅读