Javascript 在ready函数中使用变量的值

Javascript 在ready函数中使用变量的值,javascript,jquery,Javascript,Jquery,我有一个关于在ready函数中使用外部函数的值的问题。也许有一个简单的解决办法,但我还没有找到答案 我想达到以下情况。我有4个按钮: <a onmouseover="Tip('content1')" class="">Basic text1</a> <a onmouseover="Tip('content2')" class="">Basic text2</a> <a onmouseover="Tip('content3')"

我有一个关于在ready函数中使用外部函数的值的问题。也许有一个简单的解决办法,但我还没有找到答案

我想达到以下情况。我有4个按钮:

<a onmouseover="Tip('content1')" class="">Basic text1</a>    
<a onmouseover="Tip('content2')" class="">Basic text2</a>    
<a onmouseover="Tip('content3')" class="">Basic text3</a>    
<a onmouseover="Tip('content4')" class="">Basic text4</a>
现在我想知道是否可以在ready函数中使用函数
Tip()
中的
var content
变量

请注意,我知道不再推荐使用内联Javascript代码,但我被限制使用它(由于以前的实现)。因此,
onmouseover
的实现必须保持不变


这里的示例:

首先,需要对函数进行一些更改

function Tip(argument) {
    var content = argument;
    $("#results").html("Tip function: " + content);
    return content;
}
直接使用函数

$("#results2").html("Ready function: " + Tip('Pass something.'));
或者使用局部变量

$(document).ready(function(){
    var content = Tip('Pass something.');
    $("#results2").html("Ready function: " + content);
});
var content;
function Tip(argument) {
    content = argument;
    $("#results").html("Tip function: " + content);
    return content;
}

$(document).ready(function () {
    Tip('Pass something');
    $("#results2").html("Ready function: " + content);
});
或者使用全局变量

$(document).ready(function(){
    var content = Tip('Pass something.');
    $("#results2").html("Ready function: " + content);
});
var content;
function Tip(argument) {
    content = argument;
    $("#results").html("Tip function: " + content);
    return content;
}

$(document).ready(function () {
    Tip('Pass something');
    $("#results2").html("Ready function: " + content);
});
这样试试

var objTest = {
    content : null,
    Tip: function(arg) {
        this.content = arg;
        $("#results").html("Tip function: " + this.content);
    },
    getContent : function(){

        return this.content;

    }
}


$(document).ready(function()
{  
   var content = objTest.getContent();
   $("#results2").html("Ready function: "+content); // How do I get the content value in this function??

});


function Tip(argument){

    objTest.Tip(argument);
}


<a onmouseover="Tip('content1')" class="">Basic text1</a>    
<a onmouseover="Tip('content2')" class="">Basic text2</a>    
<a onmouseover="Tip('content3')" class="">Basic text3</a>    
<a onmouseover="Tip('content4')" class="">Basic text4</a>
var objTest={
内容:空,
提示:函数(arg){
this.content=arg;
$(“#results”).html(“提示函数:+this.content”);
},
getContent:function(){
返回此.content;
}
}
$(文档).ready(函数()
{  
var content=objTest.getContent();
$(“#results2”).html(“Ready function:+content);//如何在此函数中获取内容值??
});
函数提示(参数){
对象测试提示(参数);
}
基本文本1
基本文本2
基本文本3
基本文本4

嗯,这里有一个我通常使用的便宜但快速的技巧
.data()

每当您希望存储变量的值时,请将其存储在:

$("body").data("my_content", content); //Setter
&类似地检索:

$("body").data("my_content"); //Getter

有许多代码技术,但我更喜欢这种技术,因为内容在DOM中不可见,因此可以用于临时操作

就绪函数中的Tip()调用没有任何用处,因为1)没有传递参数,2)没有捕获返回值。。相反,请尝试使用全局变量,但我们不会在直接方法和局部变量方法中向Tip()传递任何参数,这不会返回吗undefined@Danton是的,你是对的。函数调用有一些问题。我不明白如何处理参数[0]。这可能与数据属性有关?arguments对象是与传递给函数的参数相对应的类似数组的对象@