繁体   English   中英

变量变成另一个变量“未定义”

[英]Variable into another variable getting “undefined”

在此代码中,变量“ link”为“ undefined”。 我不知道是什么原因。 这是我的代码:

HTML,第一次尝试:

<html>
    <head>
                <meta charset="utf-8" />
        <script src="js.js"></script>
        <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    </head>
    <body>
        <p><a onclick="doSom()">Click here!</a></p>
    </body>
</html>

JavaScript(带jQuery),第一次尝试:

var link;
var testing="Testing this <a href=\""+link+"\">variable</a> here.";
function doSom(){
    link="http://www.google.com";
    $('p').html(testing);
}

HTML,第二次尝试:

<html>
    <head>
        <meta charset="utf-8" />
        <script src="js.js"></script>
        <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    </head>
    <body>
        <p><a onclick="doSom('www.google.com.br')">Click here!</a></p>
    </body>
</html>

JavaScript(带jQuery),第二次尝试:

var link;
var testando="Testing this <a href=\""+link+"\">variable</a> here.";
function doSom(link){
    $('p').html(testing);
}

变量“ link”在上面的代码中未提供其值,仍为“未定义”。

这是有效的初始代码(但我正在对其进行优化):

第一次尝试的HTML;

JavaScript(带有jQuery):

var link;
var testingi="Testing this <a href='";
var testingii="'>variable</a> here.";
function doSom(link){
    $('p').html(testingi+link+testingii);
}

我认为完全不必为一个句子声明2个变量。

您的问题归结为您在初始化 link变量之前尝试使用它的事实。

var link; // By default, this is `undefined`
var testing="Testing this <a href=\""+link+"\">variable</a> here.";
// Your `testing` link will now contain the string `undefined`
function doSom(){
    link="http://www.google.com";
    // Since you don't update `testing` it still has "undefined" in it somewhere
    $('p').html(testing);
}

解决方案:等到初始化link变量后,再构建testing链接。

您要在链接具有值之前添加它,因此未定义。 然后定义字符串。

看这里:

var link;
 ----  this is empty at definition !!! var testing="Testing this <a href=\""+link+"\">variable</a> here.";
 function doSom(){
 link="http://www.google.com";
$('p').html(testing);
}

做这个:

var link;
var testing;
function doSom(){
link="http://www.google.com";
testing ="Testing this <a href=\""+link+"\">variable</a> here.";
$('p').html(testing);
}

对于您的代码“ tentativa 1”,未定义链接的原因是您在将值分配给链接之前将其插入了字符串测试。

更改

var link;
var testing="Testing this <a href=\""+link+"\">variable</a> here.";
function doSom(){
    link="http://www.google.com";
    $('p').html(testing);
}

var link;
function doSom(){
    link="http://www.google.com";
var testing="Testing this <a href=\""+link+"\">variable</a> here.";
    $('p').html(testing);
}

它会工作。

与“第二次尝试”相同。 移动您的字符串定义进行测试(并将其重命名为正确的名称),它应该可以正常工作。

我猜想“ JavaScript(带有jQuery):”行得通。

没错,但是您不需要定义这些变量。 这就是为什么在上一个示例中它可以正常工作的原因。 这是因为在定义“链接”之后,您要引用它。

更好的方法(定义后避免避免使用无用的变量和引用“链接”是将html分配更改为如下所示:

    link="http://www.google.com";
    $('p').html("Testing this <a href=\""+link+"\">variable</a> here.");

您的主要收获应该是,将变量放入字符串中不会保留对该变量的引用。 变量包含在字符串中时的值是最终值。 在将其赋值之前,不得将其包括在最终的字符串分配中。

暂无
暂无

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

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