简体   繁体   English

JavaScript(jquery)追加IE9的问题

[英]JavaScript (jquery) append issue with IE9

im using jquery to add a quickmenue to my website. 我正在使用jquery为我的网站添加一个quickmenue。 my source code is like this : 我的源代码是这样的:

var span = $('body').append('<span id="quickmenu">');
       $('#quickmenu').css('position','fixed')
              .css('background-color','white')
              .css('border','4px solid black')
              .css('border-left','0px solid black')
              .css('top','15px')
              .css('left','0px')
              .css('font-family','Calibri')
              .css('font-size','14px')
              .css('line-height','0px')
              .css('color','#777777')
              .css('text-align','center');

My problem on that is, everything works really fine in FF and Chrome, but IE9 isn't showing anything to me. 我的问题是,在FF和Chrome中一切正常,但IE9并没有向我显示任何内容。

So i tried some alerts() after accessing the above code...: 所以我在访问上面的代码后尝试了一些alert()...:

alert('I run'); // I run
alert(span); // [object Object]
alert($("#quickmenu")); // [object Object] 
alert(span == $("#quickmenu")); // false

So i seems to be created , but not shown?! 所以我似乎被创造了,但没有显示?!

I really have no idea what went wrong there,.... Console shows no errors in FF and IE9 我真的不知道那里出了什么问题,....控制台在FF和IE9中没有显示错误

Any ideas ?! 有任何想法吗 ?! :-/ : - /

I believe the code is running too early. 我相信代码运行得太早了。 You need to wrap your code in the document ready function: 您需要将代码包装在文档就绪函数中:

jQuery(document).ready(function() {

//your code goes here

});

I would in fact change your function to something like below: 事实上,我会将您的功能更改为以下内容:

 jQuery(document).ready(function(e) {   
    var span = $('<span id="quickmenu" >')
        .css({
            'position':'fixed',
             'background-color':'white',
             'border':'4px solid black',
             'border-left':'0px solid black',
             'top':'15px',
             'left':'0px',
             'font-family':'Calibri',
             'font-size':'14px',
             'line-height':'0px',
             'color':'#777777',
             'text-align':'center'
        })
        .appendTo($("body"));
});

As Smoki pointed out in the comments. 正如Smoki在评论中指出的那样。

Switching from: $("#target").append(my_dom_element); 切换自: $("#target").append(my_dom_element);

to: my_dom_element.appendTo("#target"); to: my_dom_element.appendTo("#target");

Seems to solve the issue. 似乎解决了这个问题。

I was using a large but unscripted element that I was importing from an external file and turning into an element in advance. 我正在使用一个大型但没有脚本的元素,我从外部文件导入并提前转换为元素。 Then the append would fail. 然后追加会失败。 Switching to using the .appendTo() method worked. 切换到使用.appendTo()方法。 So I would hazard a guess that the IE code for .append() gets confused when asked to append complex elements. 因此,当我被要求附加复杂元素时,我会猜测.append()的IE代码会混淆。 Though it could just as easily be what it was appending it to that confused it. 虽然它可以很容易地将它附加到它混淆它的东西。

(jQuery 1.8.2) (jQuery 1.8.2)

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

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