簡體   English   中英

與純JS中的jQuery代碼等效

[英]Equivalent of this jQuery code in pure JS

在純JS(沒有jQuery)中,這段代碼相當於什么?

$.each(data, function(i, item) {
    if(window.location.href.indexOf(data[i].url) > -1) {
        slt = data[i].id;
        if ($(slt).length) {
            $(slt).html(data[i].html);
        }
    }
})

我嘗試了此示例,但沒有成功。

var elements = document.querySelectorAll(data);
Array.prototype.forEach.call(elements, function(el, i){
  alert(el);
});

請你幫助我好嗎 ?

forEach()是一個數組函數,它遍歷您的數組元素並提供數據元素及其在回調中的索引。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

call()-在和上調用,用於執行功能。

假設數據是某個數組的問題的解決方案。

data.forEach(function(item,i){
    if(window.location.href.indexOf(data[i].url) > -1){
       slt = data[i].id;
       if(slt.length)
       {
            document.getElementById(slt).innerHTML = data[i].html;
       }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM