简体   繁体   English

实际上没有定义函数

[英]Function not defined, when it actually is

When I run this javascript, I get applyBefore is not defined. 当我运行此javascript时,未定义applyBefore。 I simply have 2 buttons with onclick="applyBefore();" 我只用onclick =“ applyBefore();”有2个按钮 in HTML. 在HTML中。 Here is JS: 这是JS:

(function (){

    $("div").css("border", "1px solid black");
    $("div").css("margin-top", "100px");
    $("div").css("margin-left", "50px");
    $("div").css("width", "100px");

    var input = $("input[text]").value;
    var btnLeft = $("#btnLeft");

    function applyBefore() {

        console.log("ne staa");

        var content = document.createElement("p");
        content.appendChild(document.createTextNode(input));
        $("div").prepend(content);
        content.before$("#mainDiv");

        console.log("ne staa");
    }

    function applyAfter() {

    }

}());

You have defined the function inside another function. 您已经在另一个函数中定义了该函数。 It therefore exists in the scope of that function and not the global scope. 因此,它存在于该功能的范围内,而不是全局范围。

Don't use onclick attributes. 不要使用onclick属性。 Bind your event handlers with JavaScript , and do so inside the anonymous function that you are using to limit the scope of your other variables. 将事件处理程序与JavaScript绑定在一起 ,并在用于限制其他变量范围的匿名函数内进行绑定

Since you are using jQuery: 由于您使用的是jQuery:

jQuery('button').on('click', applyBefore);

You probably want to get the value of the input correctly too (the value property exists on DOM node objects, you have a jQuery object so use the val() method) and to get that value when the button is clicked instead of storing the value the document has when it is loaded. 您可能还想正确获取输入的valuevalue属性存在于DOM节点对象上,您有一个jQuery对象,因此请使用val()方法),并在单击按钮而不是存储值获取该值。该文档在加载时具有。

The problem is that you've only defined those functions within the scope of the outer function. 问题在于您仅在外部函数范围内定义了这些函数。 If you want to use it to bind an event directly in html as in <a onclick="applyBefore();"> , you'll have to declare them outside that function: 如果要像<a onclick="applyBefore();">那样使用它直接在html中绑定事件,则必须在该函数之外声明它们:

function applyBefore() {
    var input = $("input[text]").val(); // Note the use of val()

    ...
}

function applyAfter() {

}

(function (){
    $("div").css("border", "1px solid black");
    $("div").css("margin-top", "100px");
    $("div").css("margin-left", "50px");
    $("div").css("width", "100px");
}());

Or better yet, get rid of the html event binding and do it in JavaScript: 或者更好的是,摆脱html事件绑定,并在JavaScript中完成它:

(function (){

    $("div").css("border", "1px solid black");
    $("div").css("margin-top", "100px");
    $("div").css("margin-left", "50px");
    $("div").css("width", "100px");

    input = $("input[text]").val(); // Note the use of val()
    var btnLeft = $("#btnLeft");

    function applyBefore() {
        ...
    }

    function applyAfter() {
        ...
    }

    $("#myElement").on('click', applyBefore); // bind event here
}());

Also, if you want to get the value of an input element(s) returned by $("input[text]") you should use $("input[text]").val() or possibly $("input[text]")[0].value instead of just $("input[text]").value . 另外,如果要获取$("input[text]") )返回的输入元素的值,则应使用$("input[text]").val()或可能使用$("input[text]")[0].value而不是$("input[text]").value

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

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