第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 JS 中存儲點擊事件的一些數(shù)據(jù)?

如何在 JS 中存儲點擊事件的一些數(shù)據(jù)?

qq_笑_17 2022-05-22 13:54:30
我正在用 js 制作一個分析應(yīng)用程序。我需要從被點擊的元素中獲取文本。我目前正在使用此功能獲取數(shù)據(jù)。clickfunc = function(link) {var t = link.innerText || link.textContent;console.log(t);}我在鏈接上將此函數(shù)稱為-<a href="#work" onclick='clickfunc(this)'>問題是,我需要從元素中獲取數(shù)據(jù)而不調(diào)用代碼中的函數(shù),就像在 GTM 中一樣。任何幫助表示贊賞。
查看完整描述

3 回答

?
三國紛爭

TA貢獻1804條經(jīng)驗 獲得超7個贊

在這里,因為我在 中調(diào)用了函數(shù)<a href="#work" onclick='clickfunc(this)'>,所以我需要創(chuàng)建一個不必像這樣調(diào)用的函數(shù)。

聽起來您在問如何使用現(xiàn)代事件處理,這是個好主意。

你這樣做:

  1. onclick從該標(biāo)簽中 刪除,并且:

    1. 添加在該元素創(chuàng)建后運行的腳本并為其連接一個處理程序;或者

    2. 添加掛鉤祖先元素的腳本,click然后在點擊冒泡到該祖先時檢查它是否通過該元素。(這稱為事件委托。)

這是#1的示例:

<a href="#work">this is the #work element</a>

<a href="#life">this is something else</a>

<script>

document.querySelector("a[href='#work']").addEventListener("click", function() {

    console.log(this.textContent || this.innerText);

});

</script>

請注意,必須在元素存在后評估該代碼。在現(xiàn)代瀏覽器上有幾種方法可以做到這一點:

  1. 將它放在script文檔末尾的標(biāo)簽中,就在結(jié)束</body>標(biāo)簽之前。這適用于現(xiàn)代和舊的和過時的瀏覽器。

  2. 或者將defer屬性放在script標(biāo)簽上(這假設(shè)您使用帶有 的外部腳本src;它不適用于內(nèi)聯(lián)腳本)。適用于現(xiàn)代瀏覽器。

  3. 或者將代碼包裝在DOMContentLoaded事件的處理程序中。適用于現(xiàn)代和舊版瀏覽器。

這是#2(事件委托)的示例:

<script>

document.addEventListener("click", function(e) {

    var a = e.target.closest("a[href='#work']");

    if (a) {

        // The event passed through the element on its way to the document

        console.log(a.textContent || a.innerText);

    }

});

</script>

<a href="#work">this is the #work element</a>

<a href="#life">this is something else</a>


該closest方法存在于現(xiàn)代和稍舊的瀏覽器上,但不存在于 IE11 上。如果需要使用兼容 IE11 的代碼:


document.addEventListener("click", function(e) {

    var a = e.target;

    while (a && a.nodeType === 1) {

        if (a.tagName === "A" && a.getAttribute("href") === "#work") {

            // The event passed through the element on its way to the document

            console.log(a.textContent || a.innerText);

            break;

        }

        a = a.parentNode;

    }

});

在評論中你問:


我var a = e.target.closest("a[href]")可以引用所有具有 href 的標(biāo)簽嗎?


是的,就是這樣。e.target.closest("a[href]")將匹配具有從其祖先開始并向上移動的屬性的第一個a元素。只會對以 .開頭的元素執(zhí)行此操作。例如,下面是上面的示例,其中兩個元素記錄了它們的文本,而第三個元素不記錄,因為它不匹配:hrefe.targete.target.closest("a[href^='#']")ahref#href="#xyz"href="http://example.com"


<script>

document.addEventListener("click", function(e) {

    var a = e.target.closest("a[href^='#']");

    if (a) {

        // The event passed through the element on its way to the document

        console.log(a.textContent || a.innerText);

    }

});

</script>

<div><a href="#work">this is the #work element</a></div>

<div><a href="#life">this is the #life element</a></div>

<div><a href="http://example.com">this is the http://example.com element</a></div>



查看完整回答
反對 回復(fù) 2022-05-22
?
RISEBY

TA貢獻1856條經(jīng)驗 獲得超5個贊

我已將代碼添加到腳本標(biāo)簽中,并將其放入頭標(biāo)簽中。因此,一旦加載了 dom,它將為所有具有 href 的標(biāo)簽附加一個點擊偵聽器。


<!DOCTYPE html>

<html>


<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script>

        window.addEventListener('DOMContentLoaded', (event) => {

            document.querySelectorAll('a[href]').forEach(function (elms) {

                elms.addEventListener('click', function (link) {

                    var t = link.innerText || link.textContent;

                    console.log(t);

                });

            });

        });

    </script>

</head>


<body>

  <a href="#work">

  <a href="#abc">

</body>


</html>


查看完整回答
反對 回復(fù) 2022-05-22
?
手掌心

TA貢獻1942條經(jīng)驗 獲得超3個贊

我可以建議你需要像 global watcher 這樣的東西嗎?


document.addEventListener('click', watchAnchorClicks);


function watchAnchorClicks(event){

  if (event.target.tagName === 'A'){

    var anchor = event.target;

    console.log('Click on anchor with text ' + anchor.innerText + ' and href ' + anchor.href);

  }

}

<p>Paragraph with <a href="#anchor">anchor</a> in.</p>

<p>Yet another paragraph with other <a href="#another">other anchor</a> in.</p>


查看完整回答
反對 回復(fù) 2022-05-22
  • 3 回答
  • 0 關(guān)注
  • 210 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號