猿问

如何将 Jquery 代码转换为 GithubPages 的 Javascript

我是 javascript 的新手并且还在学习。我有一个按区域划分的国家/地区的svg 地图,我想在将鼠标悬停在每个区域上时显示一些信息。


下面的代码在本地运行时运行良好(jquery),但当将其作为 Github 页面上传到 Github 时,它不起作用。


我想要一些关于如何将我的代码的以下部分转换为 javascript 的建议。(我尝试过使用 addEventListener 和 body.appendChild 但没有成功)


$('#regions > *').mouseover(function (e) {

    var region_data = $(this).data('region');


    // Info box informations

    $('<div class="info_box">' + region_data.region_name + '<br>' + '</div>').appendTo('body');

});


// Show info box when mousemove over a region

$('#regions > *').mousemove(function(e) {

    var mouseX = e.pageX, 

        mouseY = e.pageY;


    // Position of information box

    $('.info_box').css({

        top: mouseY-50,

        left: mouseX+10

    });

}).mouseleave(function () {

   $('.info_box').remove();

});

我尝试过如下内容:


var mapRegion = document.querySelectorAll("#regions > *");


    mapRegion.forEach(function(reg){

        

        reg.addEventListener('mouseover', function(el){  

            var perif_data = this.data('region');

            document.body.appendChild('<div class="info_box">' + region_data.region_name + '<br>' + '</div>');    

        }); 

        

        reg.addEventListener('mousemove', function(e){

            var mouseX = e.pageX; 

            var mouseY = e.pageY; 

            // Position of information box

            document.querySelector('info_box').style.top = mouseY-50;

            document.querySelector('info_box').style.css = mouseX+10;

        });

        

         reg.addEventListener('mouseleave', function(){

            reg.classList.remove('.info_box');

        });

        

    });

但我正在使用控制台:


this.data 不是函数

document.querySelector(...) 为空


互换的青春
浏览 97回答 3
3回答

至尊宝的传说

现代 JavaScript 使这变得非常容易。您只需要迭代querySelectorAll调用的结果并将侦听器添加到每个孩子。此外,看起来您的数据是一个 JSON 对象,因此您可能需要使用JSON.parse.我建议不要每次都销毁和重新创建信息框。只需使用最新信息更新它并根据您当前是否将鼠标悬停在某个区域上来隐藏/显示它。Array.from(document.querySelectorAll('#regions > *')).forEach(region => {&nbsp; region.addEventListener('mouseover', e => {&nbsp; &nbsp; const infobox = document.querySelector('.info_box')&nbsp; &nbsp; const regionData = JSON.parse(e.target.dataset.region)&nbsp; &nbsp; infobox.textContent = regionData.region_name&nbsp; &nbsp; infobox.classList.toggle('hidden', false)&nbsp; })&nbsp; region.addEventListener('mousemove', e => {&nbsp; &nbsp; const infobox = document.querySelector('.info_box')&nbsp; &nbsp; if (!infobox.classList.contains('hidden')) {&nbsp; &nbsp; &nbsp; Object.assign(infobox.style, {&nbsp; &nbsp; &nbsp; &nbsp; top: (e.pageY - 50) + 'px',&nbsp; &nbsp; &nbsp; &nbsp; left: (e.pageX + 10) + 'px'&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; })&nbsp; region.addEventListener('mouseleave', e => {&nbsp; &nbsp; const infobox = document.querySelector('.info_box')&nbsp; &nbsp; infobox.classList.toggle('hidden', true)&nbsp; })}).info_box {&nbsp; position: absolute;&nbsp; top: 0;&nbsp; left: 0;&nbsp; border: thin solid grey;&nbsp; background: #FFF;&nbsp; padding: 0.25em;}.info_box.hidden {&nbsp; display: none;}.region {&nbsp; display: inline-block;&nbsp; width: 5em;&nbsp; height: 5em;&nbsp; line-height: 5em;&nbsp; text-align: center;&nbsp; margin: 0.5em;&nbsp; border: thin solid grey;}<div id="regions">&nbsp; <div class="region" data-region='{"region_name":"A"}'>Section A</div>&nbsp; <div class="region" data-region='{"region_name":"B"}'>Section B</div>&nbsp; <div class="region" data-region='{"region_name":"C"}'>Section C</div></div><div class="info_box hidden"></div>

慕虎7371278

您可以通过实现一个addListeners遍历所有元素并应用各种事件侦听器的函数来简单地做到这一点。const addListeners = (selector, eventName, listener) => {&nbsp; Array.from(document.querySelectorAll(selector)).forEach(el => {&nbsp; &nbsp; typeof eventName === 'string' && listener != null&nbsp; &nbsp; &nbsp; ? el.addEventListener(eventName, listener)&nbsp; &nbsp; &nbsp; : Object.keys(eventName).forEach(name =>&nbsp; &nbsp; &nbsp; &nbsp; el.addEventListener(name, eventName[name]))&nbsp; })}addListeners('#regions > *', {&nbsp; mouseover: e => {&nbsp; &nbsp; const infobox = document.querySelector('.info_box')&nbsp; &nbsp; const regionData = JSON.parse(e.target.dataset.region)&nbsp; &nbsp; infobox.textContent = regionData.region_name&nbsp; &nbsp; infobox.classList.toggle('hidden', false)&nbsp; },&nbsp; mousemove: e => {&nbsp; &nbsp; const infobox = document.querySelector('.info_box')&nbsp; &nbsp; if (!infobox.classList.contains('hidden')) {&nbsp; &nbsp; &nbsp; Object.assign(infobox.style, {&nbsp; &nbsp; &nbsp; &nbsp; top: (e.pageY - 50) + 'px',&nbsp; &nbsp; &nbsp; &nbsp; left: (e.pageX + 10) + 'px'&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; },&nbsp; mouseleave: e => {&nbsp; &nbsp; const infobox = document.querySelector('.info_box')&nbsp; &nbsp; infobox.classList.toggle('hidden', true)&nbsp; }}).info_box {&nbsp; position: absolute;&nbsp; top: 0;&nbsp; left: 0;&nbsp; border: thin solid grey;&nbsp; background: #FFF;&nbsp; padding: 0.25em;}.info_box.hidden {&nbsp; display: none;}.region {&nbsp; display: inline-block;&nbsp; width: 5em;&nbsp; height: 5em;&nbsp; line-height: 5em;&nbsp; text-align: center;&nbsp; margin: 0.5em;&nbsp; border: thin solid grey;}<div id="regions">&nbsp; <div class="region" data-region='{"region_name":"A"}'>Section A</div>&nbsp; <div class="region" data-region='{"region_name":"B"}'>Section B</div>&nbsp; <div class="region" data-region='{"region_name":"C"}'>Section C</div></div><div class="info_box hidden"></div>

智慧大石

//Get the body for Adding and removing the info_boxconst body = document.querySelector("body");//Get All Descendants of #Regionsconst elements = document.querySelectorAll("#regions > *");//Create the info_box Elementconst infoBoxElement = document.createElement("div");//Set the classinfoBoxElement.className = "info_box";//Iterate over each descendant of Regionselements.forEach((element) => {&nbsp; //Let's add MouseOver Event&nbsp; element.addEventListener("mouseover", (e) => {&nbsp; &nbsp; //get the "data-"" of the element and Parse it&nbsp; &nbsp; const regionData = JSON.parse(element.dataset.region);&nbsp; &nbsp; //Let's reuse the infoBoxElement and Assign the innerHTML&nbsp; &nbsp; infoBoxElement.innerHTML = regionData.region_name + "<br>";&nbsp; &nbsp; //Appending the infoBoxElement to the Body&nbsp; &nbsp; body.append(infoBoxElement);&nbsp; });&nbsp; //Let's add MouseMove Event&nbsp; element.addEventListener("mousemove", (e) => {&nbsp; &nbsp; const mouseX = e.pageX,&nbsp; &nbsp; &nbsp; mouseY = e.pageY;&nbsp; &nbsp; //Get the Infobox HTML element&nbsp; &nbsp; const infoBox = document.getElementsByClassName("info_box")[0];&nbsp; &nbsp; //Lets add the css Style&nbsp; &nbsp; infoBox.style.top = mouseX - 50;&nbsp; &nbsp; infoBox.style.top = mouseY + 10;&nbsp; });&nbsp; //Let's add MouseLeave Event&nbsp; element.addEventListener("mouseleave", (e) => {&nbsp; &nbsp; //Get the Infobox HTML element&nbsp; &nbsp; const infoBox = document.getElementsByClassName("info_box")[0];&nbsp; &nbsp; //Lets get rid of it&nbsp; &nbsp; infoBox.remove();&nbsp; });});
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答