开发者

jquery attr not working in chrome?

开发者 https://www.devze.com 2023-02-12 10:58 出处:网络
var select_tag = $(\'<select></select>\').attr( { onchange: \'createDiscipline()\', class: \'select_menu\'}
var select_tag = $('<select></select>').attr( {
    onchange: 'createDiscipline()',
    class: 'select_menu'}
).fadeIn('slow');

This works in FF but not Chrome. What happens in Chrome is that the class is set, but the createDisicipline() isn't.

Is this the wrong way to add multiple attrs to a tag? It does work in FF :/

ENTIRE CODE:

var select_tag = $('<select>', {
                    onchange: createDiscipline,
                    'class': 'select_menu'}
                ).fadeIn('slow');
            for(i = 0; i < c_data.lengt开发者_如何转开发h; i++) {
                select_tag.append('<option>' + c_data[i] + '</option>');
            }

            //wrap select_tag in div
            var div_buffer = $('<div class=\'select_buffer\'></div>');
            var div_container = $('<div class=\'select\'></div>');
            div_buffer.append(select_tag).wrap('<div class=\'select_buffer\' />');
            div_container.append(heading);
            div_container.append(div_buffer);

            //Append to the page after right_content id
            $('#right_content').append(div_container)


You should definitely not try to bind an event handler like this. Do it the unobtrusive way:

var select_tag = $('<select>', {
    change:  createDiscipline,
    'class': 'select_menu'
}).appendTo(document.body).fadeIn('slow');

Notice my changes. I wrapped class in quotes because it is a keyword in InternetExplorer and may break your code otherwise. Also I appended the newly created <select> node to the document.body.

Demo: http://jsfiddle.net/vM5Hp/


Some time it’s very frustrated that jQuery .attr not working with Chrome.
    Here I am giving you solution for this.
    Try:
    $(window).load(function () {
    //your script or function
    });
    Rather then
    $(document).ready(function () {
    //your script or function
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号