简体   繁体   English

如何使用jQuery更改插入HTML文档中的JavaScript变量值

[英]How do I change a JavaScript variable value, inserted in a HTML document, using jQuery

My objective is that when the mouse pointer is on an image (we'll call it "IMAGEN"), a text would appear at some other part of the screen. 我的目标是,当鼠标指针位于图像上(我们称其为“ IMAGEN”)时,文本会出现在屏幕的其他部分。

I'm trying to do this by using in the HTML (Assume that the CSS has the right code, making the div (we'll call it "DESCRIPCION") faded away from the start) 我正在尝试通过在HTML中使用(假设CSS具有正确的代码,使div(我们称其为“ DESCRIPCION”)从一开始就消失了)来做到这一点。

<div id="DESCRIPCION"> <script> document.write(des); </script></div>

meanwhile in the .js file, IF I write: 同时在.js文件中,如果我这样写:

var coop = "lalala this is some text";
var des = coop;

// jQuery here. If I put the mouse over IMAGEN, DESCRIPCION fades in. 

$(document).ready(function(){
$("#IMAGEN").mouseenter(function(){
    $("#DESCRIPCION").fadeTo('fast',1.0); 
}); 
$("#IMAGEN").mouseleave(function(){
    $("#DESCRIPCION").fadeTo('fast',0);
});
});

That works just fine. 那很好。 and on the browsers appears the "lalala this is some text" text. 并在浏览器上显示“拉拉拉这是一些文字”文字。 But obviously I want to use more images and more descriptions BUT the same DIV. 但是很明显,我想使用更多图像和更多描述,但要使用相同的DIV。

So I tried putting the "var des = coop" inside the jQuery function (so I can repeat the code each time of each image), but this is not working. 因此,我尝试将“ var des = coop”放入jQuery函数(这样我就可以在每个图像的每次重复执行代码),但这是行不通的。

var coop = "lalala this is some text";

// jQuery here. If I put the mouse over IMAGEN, DESCRIPCION fades in. 

$(document).ready(function(){
$("#IMAGEN").mouseenter(function(){
    var des = coop;
$("#DESCRIPCION").fadeTo('fast',1.0); 
}); 
$("#IMAGEN").mouseleave(function(){
    $("#DESCRIPCION").fadeTo('fast',0);
});
});

Can you please help me with this? 你能帮我吗? I assumed the jQuery var value change is made some different way. 我以为jQuery var值更改采用了不同的方式。 I tried to look into that but I have found nothing. 我试图调查一下,但是什么也没发现。 :/ :/

Once document.write executes that code is finished. 一旦document.write执行,该代码即告完成。 It isn't going to update based on des having a new value. 它不会根据具有新值的des进行更新。

What you can do is reassign it using $('#DESCRIPCION').text() . 可以使用$('#DESCRIPCION').text()重新分配它。 The first description will be populated, but any new data should be written using .text() (referencing the div to target) 将填充第一个描述,但是任何新数据都应使用.text()编写(将div引用为目标)


Changes around a bit to be more dynamic: 进行一些更改以使其更加动态:

<img src="http://placehold.it/100&text=Image1" data-desc="Image 1 description" />
<img src="http://placehold.it/100&text=Image2" data-desc="Image 2 description" />
<img src="http://placehold.it/100&text=Image3" data-desc="Image 3 description" />
<img src="http://placehold.it/100&text=Image4" data-desc="Image 4 description" />
<div id="DESCRIPCION"></div>

And the JS: 和JS:

$(function(){
    var $DESCRIPCION = $('#DESCRIPCION');
    $('img').hover(function(){
        var desc = $(this).data('desc');
        $DESCRIPCION.text(desc).fadeTo('fast', 1.0);
    },function(){
        $DESCRIPCION.fadeTo('fast', 0);
    });
});

Exmaple 枫树

No need for the 不需要

document.write(des);

Just use this jQuery. 只需使用此jQuery。

 $(document).ready(function(){
   $("#IMAGEN").hover(function(){
      $("#DESCRIPCION").fadeTo('fast',1.0); 
      $("#DESCRIPCION").text('what you want to be displayed'); 
   }); 
   $("#IMAGEN").mouseout(function(){
      $("#DESCRIPCION").fadeTo('fast',0);
      $("#DESCRIPCION").text(''); 
  });
});

HTML: HTML:

<img class="IMAGEN" src="whatever" data-description="lalala this is some text" />
<img class="IMAGEN" src="whatever" data-description="some other text" />
<div id="DESCRIPCION"></div>

JS: JS:

jQuery(function($){
    $(".IMAGEN").mouseenter(function(){
        $("#DESCRIPCION")
            .text( //change description
                $(this).data('description') //get value of data-description attribute
            )
            .stop(true,false) //stop current animation
            .fadeTo('fast',1.0);
    }).mouseleave(function(){
        $("#DESCRIPCION")
            .stop(true,false)
            .fadeTo('fast',0);
    });
});

Another way to do this, since you have jQuery would be the tooltip widget in the jQuery UI library 这样做的另一种方法是,由于您拥有jQuery,因此它是jQuery UI库中的工具提示小部件。

http://jqueryui.com/tooltip/ http://jqueryui.com/tooltip/

then 然后

<img src="http://placehold.it/100&text=Image1" title="Image 1 description" />
<img src="http://placehold.it/100&text=Image2" title="Image 2 description" />
<img src="http://placehold.it/100&text=Image3" title="Image 3 description" />
<img src="http://placehold.it/100&text=Image4" title="Image 4 description" />

And your js is simply 而你的js就是

$(document).tooltip();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM