为了账号安全,请及时绑定邮箱和手机立即绑定

将 document.querySelector 或 document.querySelector

将 document.querySelector 或 document.querySelector

四季花海 2023-11-12 22:15:06
我正在尝试减少 jQuery 的使用,但有时我仍然需要使用 jQuery 中的方法。document.querySelector是否可以将或 的返回值转换document.querySelectorAll为 jQuery 对象,或将 jQuery 选择转换为 document.querySelector() 或 document.querySelectorAll() 将返回的 Element 或 NodeList 对象?重新进行选择似乎比使用已有的内容更昂贵。这是一个示例document.querySelector:let qsOutput = document.querySelector('#my_id');//now if I want to use jQuery's .attr (just as an example) qsOutput.attr("alt", "the new alt attribute");  //.attr() is a jquery function我该怎么做?我是否只需要再次选择并浪费原始捕获的资源?我不是问如何更改普通 JavaScript 中的属性,我试图减少 jQuery 的使用,但仍然需要时不时地使用它,但是当我已经有了一个 through 时,用它进行新的选择似乎是document.querySelector这样浪费。如果可能的话,我想重用返回document.querySelector并将其“升级”为jQuery。
查看完整描述

1 回答

?
慕森卡

TA贡献1806条经验 获得超8个赞

您可以将一个或多个元素传递给 jQuery 函数。

$(qsOutput).attr("alt", "the new alt attribute");


查看完整回答
反对 回复 2023-11-12
  • 1 回答
  • 0 关注
  • 60 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信