变量和jQuery的:如何捕获价值和使用它们(第3部分) [英] variables and jquery: how capture value and use them (part 3)

查看:77
本文介绍了变量和jQuery的:如何捕获价值和使用它们(第3部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ok guys,
我有一个关于变量和jquery的问题的最后一篇文章:如何捕获值并使用它们(第3部分)。你可以找到关于寻找变量和jquery的其他问题:如何捕获值和使用它们(第1部分)变量和jquery:如何捕获值和使用它们(第2部分)

我必须这样做:
1.从ul-li列表中捕获一个值;
2.将它插入一个全局变量( not have been );
3.将这个变量用于另一个点击功能。



现在我展示我的解决方案代码(在社区的帮助下),但我想以优化它。



我有一个包含事件鼠标悬停的城市列表:

 < ul id =country_listonmouseover =cl();> 
< li>< a id =pulsante1href =#>罗马< / a>
< li>< a id =pulsante2href =#> Milano< / a>
< li>< a id =pulsante3href =#> Venezia< / a

这是我的CSS:

  .selected 
{
background-color: #FFFFFF;
}

开始该功能,并带有注释:

 函数cl(){
$('。map')。maphilight(); //调用插件来照亮地图

$('#country_list li a')。mouseover(function(e){//在鼠标悬停在列表上

//在鼠标上改变列表元素的背景颜色是
$(e.target).addClass('selected');

//将id值放入变量。我将捕获pulsante1例如更多#
var $ regionMap ='#'+ e.target.id;

//将值
var $ variab ='#'+ e.target.innerHTML;

//控制
// alert(regionMap);
// alert(variab);

//这部分是用于突出显示某些特定区域(城市地区)的插件,形式如下...
$($ regionMap).mouseover(function(a){
$( $ variab).mouseover();
));
$($ regionMap).mouseout(function(a){
$($ variab).mouseout();
});
});
// ...这里

//删除背景颜色
$('#country_list li a')。mouseout(function(e){
$( e.target).removeClass('selected');
});

// mousedown函数。
$('#country_list li a')。mousedown(函数(e){

// e.target是您点击的元素,给我HTML值(罗马例如)

var $ variabile = e.target.innerHTML;

var $ alfa ='#'+ $ variabile; // Roma现在是#Roma

// alert(You entered:+ $ alfa); //控制

//控制对话框($ alfa); //这个调用一个对话框函数给出它的* $ alfa *值
});
}

好的。这是我的问题:


  1. 我不喜欢在这里使用onmouseover < ul id = country_listonmouseover =cl();> 但它看起来并没有用其他方式工作('#country_list')。 >


  2. 我想为函数cl()分割相对于mousedown的部分。为此,我需要两个全局变量,其值为:



    a。 id li (例如 pulsante1 )。对我来说,这应该是 regionMap
    b。 姓名(例如 Roma )。对我来说,这应该是 variab


但我不知道如何获取全局变量!
我已经在< script> 之间的< head>< / head> regionMap = ;我已经在前面尝试了惠普$,并且在很多其他方面,但我所有的尝试都没有实现。 ..

所以你对我有帮助吗?



谢谢


而不是使用onmouseover属性,最好将mouseover事件绑定到jQuery对象。你可以通过使用下面的代码来实现:

  $('ul#country_list')。bind('mouseover',function (){//做你想做的事情在鼠标上}}; 

您也可以先定义一个函数,然后使用它作为bind函数的第二个参数。例如:

  var cl = function(){// do something}; 
$('ul#country_list')。bind('mouseover',cl);

然后你的第二个问题,我不确定你的意思是在mousedown的部分相对。
但是对于全局变量,你可以用不同的方法来做到这一点。



我总是喜欢做什么(注意这是一个不是标准的例子) :



在窗口变量中创建一个空对象,例如:

  window.globals = {}; 
函数test(){
window.globals.test ='这是无处不在!';
}
test();
console.log(window.globals);


ok guys, I have had a last post about the question variables and jquery: how capture value and use them (part 3). You can find other questions about looking for variables and jquery: how capture value and use them (part 1) and variables and jquery: how capture value and use them (part 2).

I had to do: 1. capture a value from an ul-li list; 2. insert it into a global variable (not has been successful); 3. use this variable for another click function.

Now I present my solution code (with the helps of the community) that sounds, but I would like to optimize it.

I have a ul-li list of cities with an event mouseover:

<ul id="country_list" onmouseover="cl();">
        <li><a id="pulsante1" href="#">Roma</a>
        <li><a id="pulsante2" href="#">Milano</a>
        <li><a id="pulsante3" href="#">Venezia</a

this is my CSS:

.selected 
     {
     background-color: #FFFFFF;
     }

begin the function, with comment:

 function cl(){
     $('.map').maphilight(); // call a plug-in to illuminate the maps

       $('#country_list li a').mouseover(function(e){ //on mouseover over the list

    // change background color at the element of the list over the mouse is 
       $( e.target ).addClass('selected');  

    //put id value in a variable. I'm going to capture pulsante1 for example more #
        var $regionMap = '#' + e.target.id; 

    // put the value 
        var $variab = '#'+ e.target.innerHTML;

           //control
        //alert(regionMap);
        // alert (variab);

// this part is the plugin for highlight some particular areas (the cities' areas). form here to...
          $($regionMap).mouseover(function(a) {
            $($variab).mouseover();
            });
            $($regionMap).mouseout(function(a) {
            $($variab).mouseout();
            }); 
        });
//... here

//remove background color 
        $('#country_list li a').mouseout(function(e){
         $( e.target ).removeClass('selected');
        });     

// mousedown function. 
         $('#country_list li a').mousedown( function(e) {

         // e.target is the element you clicked, give me HTML value (Roma for example) 

        var $variabile = e.target.innerHTML;

    var $alfa= '#' + $variabile;  // Roma is now #Roma  

      // alert("You entered: " + $alfa); //control

    //control   dialog($alfa); // this call a dialog function giving its *$alfa* value
     });
    }

ok. this are my problems:

  1. I don't like use onmouseover here <ul id="country_list" onmouseover="cl();"> but it doesn't seem work in other way ('#country_list').mouseover doesn't work Ideas?

  2. I would like divide the function cl() for the part relative at mousedown. For this I need have two global variables with the value of:

    a. id li (for example pulsante1). For me this should be regionMap. b. name li (for example Roma). For me this should be variab.

but I don't know the way to obtain a global variable! I have declared them in the <head></head> between <script> regionMap=""; variab="";

I have tryed whit $ in front, and in a lot of other ways but all my tentatives have been unfulfilled...

So you have help for me?

Thanks

解决方案

For your first problem

instead of using the onmouseover attribute it is better to bind the mouseover event to the jQuery object. You can do that by using the following code:

$('ul#country_list').bind('mouseover', function () { // do what you want to do on mouse over });

You can also first define a function and then use that as the second argument for the bind function. For example:

var cl = function () { // do something };
$('ul#country_list').bind('mouseover', cl);

Then your second problem, im not sure what you mean with "the part relative at mousedown". But for global variables you can do this in different ways.

What I always like to do (note this is an example of doing it not a standard):

Create an empty object within the window variable for example:

window.globals = {};
function test () {
    window.globals.test = 'this is available everywhere!';
}
test();
console.log(window.globals);

这篇关于变量和jQuery的:如何捕获价值和使用它们(第3部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆