Javascript 将jQuery$(this)重构为Vanila Js
我将jQuery重构为Vanila JS,但我不知道如何实现 假设我有以下jQuery代码Javascript 将jQuery$(this)重构为Vanila Js,javascript,jquery,Javascript,Jquery,我将jQuery重构为Vanila JS,但我不知道如何实现 假设我有以下jQuery代码 $('#simple').find('.single').each(function(){ // I want to refactor $(this) into Vanilla JS var txt = $(this).find('.text').text(); }) 假设我将jQuery代码重构为vanillaJS,但它不起作用 document.getElementById
$('#simple').find('.single').each(function(){
// I want to refactor $(this) into Vanilla JS
var txt = $(this).find('.text').text();
})
假设我将jQuery代码重构为vanillaJS,但它不起作用
document.getElementById('simple').queryAll('.single').forEach(function(){
// It does not work this line
var txt = document.getElementById(this).find('.text').text();
})
真正的语法是什么?在Vanilla Js中,$(this)的等价物是什么?您可以转换此Jquery代码
$('#simple').find('.single').each(function(){
// I want to refactor $(this) into Vanilla JS
var txt = $(this).find('.text').text();
})
在香草js如下
document.querySelectorAll('#simple .single').forEach(function(elm){
var textElm = elm.querySelector('.text');
var txt = textElm && textElm.innerText;
})
我希望这将对您有所帮助:)
queryAll
无效语法错误消息清楚地告诉您queryAll
不是一个函数;这是querySelectorAll
。在Vanilla JS中还有find()
或text()
。这两个queryAll
或queryselectoral
都对我有用。如何将$(这个)重构为Vanilla Js。这正是我想要的。谢谢