简体   繁体   English

onclick属性无法正常工作

[英]onclick attribute not working as expected

I have a jquery file as follow : 我有一个jquery文件,如下所示:

$('.font').click(function(){
        for(i=0;i<FontArray.length;i++){
            var font = FontArray[i]; 
            $('#view_container').append("<button class='b_font' id='"+font+"' onclick='saveMe("+font+");'>"+font+"</button>");
            $('.'+font).css('font-family',font);
        }
        // event.preventDefault();
    });

And the function saveMe is defined as follow : 函数saveMe定义如下:

function saveMe(font){
        alert(font);
    }

But when i click any of the button i get the following error : Uncaught ReferenceError: saveMe is not defined 但是,当我单击任何按钮时,都会出现以下错误: 未捕获的ReferenceError:saveMe未定义

Can anyone help me on this ? 谁可以帮我这个事 ?

Thanks in advance! 提前致谢!

The line 线

onclick='saveMe("+font+");'

renders as 呈现为

onclick='saveMe(arial);'

So when you click on it, it is looking for a variable arial not the string "arial" . 因此,当您单击它时,它正在查找变量arial而不是字符串"arial" You need to quote it. 您需要引用它。

onclick='saveMe(\""+font+"\");'

Now for saveMe not being defined, that needs to have more context. 现在,对于未定义saveMe的情况,需要具有更多上下文。 When is saveMe defined? saveMe何时定义? If it is inside of a document ready/window onload method, than you need to put it outside of it so it is available in window scope. 如果它在文档就绪/窗口onload方法内部,则需要将其放在文档外部,以便在窗口范围中可用。

And the line 和线

$('.'+font).css('font-family',font);

Should probably be 应该是

$('#'+font).css('font-family',font);

The most important thing is where the saveMe function is defined. 最重要的是定义saveMe函数的位置。 onclick has only access to global scope and that's there the fuction would have to be for this solution to work. onclick只能访问全局范围,因此该解决方案必须具有功能。

That'd be a quick workaround: 那是一个快速的解决方法:

window.saveMe = function (font){
    alert(font);
}

However, you should note that cluttering global scope is usually undesirable and quickly leads to errors. 但是,您应该注意,混乱的全局范围通常是不希望的,并且会迅速导致错误。 It's better to attach events to selected elements by using .click() . 最好使用.click()将事件附加到选定的元素。 In your code - you can attach the element and then bind click event to it. 在代码中-您可以附加元素,然后将click事件绑定到该元素。

var $button = $("<button class='b_font' id='"+font+"' onclick='saveMe("+font+");'>"+font+"</button>");
$('#view_container').append($button);
$button.click(function () {
    alert($(this).attr('id'));
});

EDIT: As epascarello also pointed out - the string is not escaped properly 编辑:正如epascarello也指出-字符串无法正确转义

I'm guessing saveMe is out of scope, you probably put it inside the DOM ready handler. 我猜saveMe不在范围内,您可能将其放入DOM ready处理程序内。

As you're using jQuery, why not use it, and get rid of those inline event handlers 在使用jQuery时,为什么不使用它,并摆脱那些内联事件处理程序

$(function() {

    $('.font').click(function() {
        $('#view_container').append(
            $.map(FontArray, function(index, font) {
                return $('<button />', {
                            'class' : 'b_font',
                            id      : font,
                            on      : { click : saveMe },
                            css     : { fontFamily : font }
                })
            })
        )
    });

    function saveMe() {...}

});

You could simply add this code and get rid of the code in the markup: 您可以简单地添加以下代码,并删除标记中的代码:

$('#view_container').on('click','.b_font',function(){
  saveMe($(this).text());
});

OR perhaps better make this change: 或者也许更好地进行此更改:

$('#view_container').append("<button class='b_font' id='"+font+"' data-fontsave='"+font+"'>"+font+"</button>");

then this works and is less prone to error perhaps: 那么这可行,并且不太容易出错:

$('#view_container').on('click','.b_font',function(){
  saveMe($(this).data('fontsave'));
});

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

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