简体   繁体   English

jQuery 中奇怪的变量范围

[英]strange variable scope in jQuery

I know scope in javascript in sometimes tough but this time I suspect the issue may be jQuery execution order.我知道 javascript 中的范围有时很难,但这次我怀疑问题可能是 jQuery 执行顺序。 In the following code I try to define a simple HTML element (simulates a button) in javascript and pass different text to it when mounting it in HTML using jQuery:在下面的代码中,我尝试在 javascript 中定义一个简单的 HTML 元素(模拟一个按钮),并在使用 jQuery 将其安装在 HTML 中时将不同的文本传递给它:

 var name; var buttonsecondary = '<div class="buttonsecondary clicked"><p>'+name+'</p></div>';
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="content-item" id="things4"> <a href="aFabrica.html"> <div class="itemHome"> <div class="bg" id="buttonsecondaryfabrica"></div> <script> $(document).ready(function() { var name = "A Fábrica"; $("#buttonsecondaryfabrica").after(buttonsecondary) }) </script> </div> </a> </div> <div class="content-item"> <a href="lojas.html"> <div class="itemHome"> <div class="bg" id="buttonsecondaryloja"></div> <script> $(document).ready(function() { var name = "Loja"; $("#buttonsecondaryloja").after(buttonsecondary) }) </script> </div> </a> </div>

The problem is that I get the same text on both buttons: "Store" although in the first alert getting "Street" and in the second "Store"... Does anyone know how to explain it?问题是我在两个按钮上都得到了相同的文本:“商店”虽然在第一个警报中得到“街道”,在第二个“商店”中......有谁知道如何解释它?

The problem is that the buttonsecondary variable already contains the final HTML of the button because it's merely a concatenated string.问题是buttonsecondary变量已经包含了按钮的最终 HTML,因为它只是一个连接的字符串。

You need to generate the desired HTML each time:您每次都需要生成所需的 HTML:

function generateButton(name)
{
    return '<div class="buttonsecondary clicked"><p>' + name + '</p></div>';
}

Then:然后:

var name = "A Fábrica";
$("#buttonsecondaryfabrica").after(generateButton(name));

And

var name = "Loja";
$("#buttonsecondaryloja").after(generateButton(name));

In your original code, you are creating a string with variables that are changed later on.在您的原始代码中,您正在创建一个带有稍后更改的变量的字符串。 When you change the variables, the string does not get updated because the variables are not bound.更改变量时,字符串不会更新,因为变量未绑定。 You need to create a new string if you want to pass in a new value for the name .如果要为name传入一个新值,则需要创建一个新字符串。

Change this:改变这个:

var buttonsecondary = '<div class="buttonsecondary clicked"><p>'+name+'</p></div>';

To this:对此:

function createSecondaryButton(name) {
    return '<div class="buttonsecondary clicked"><p>' + name + '</p></div>';
}

Or, since you are using jQuery:或者,由于您使用的是 jQuery:

function createSecondaryButton(name) {
    return $('<div>').addClass('buttonsecondary clicked')
        .append($('<p>').text(name));
}

Then simply call the function:然后简单地调用函数:

$("#buttonsecondaryfabrica").after(createSecondaryButton('A Fábrica'));
$("#buttonsecondaryloja").after(createSecondaryButton('Loja'));

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

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