jQuery上的IE7 / IE8 Javascript错误.remove()

时间:2011-05-22 19:18:01

标签: javascript jquery internet-explorer list html-lists

所有在Firefox / Chrome /等中工作的东西。但在IE7& IE8我在jQuery .remove()(v = 1.5.2)函数上遇到错误。 IE错误是:

Object does not support this property or method.

使用以下代码通过jQuery生成list元素:

var add_term = $(this).text();
var new_list_element = $('<li><span><input name="med[' + count + ']" class = "new_med_field" value="' +add_term +'" readonly="readonly"></span></li>').hide();
$('div#create_right form ul').append(new_list_element);

然后我尝试使用以下内容删除相同的元素(点击):

var $tgt ='';
$('#create_right form').click(function(event){
    $tgt = $(event.target);
    //Remove different portions depending on which element is selected
    if ($tgt.is('li') || $tgt.is('span') || tgt.is('input[class="new_med_field"]')) {
        $tgt.closest('li').remove();
        count--;        
    }
});

链接到网站:http://refillwizardstage.heroku.com/refill/create

3 个答案:

答案 0 :(得分:2)

我想说这是一个错误。在sizzle中有一个未处理的异常,它只发生在不支持querySelectorAll的浏览器中(如兼容模式下的IE7或IE8 +)。

type的属性处理程序接收一个不是元素节点的参数(在这种情况下,它是一个DOMDocumentFragment,看起来像是已移除节点的副本,没有方法getAttribute)。

只要你的功能似乎像预期的那样工作,你就可能发现这个错误。

在嵌入的jquery.js文件后面右键:

<script>
 jQuery.find.selectors.attrHandle.type=function( elem ) {   
       try{return elem.getAttribute( "type" );}catch(e){return'';}
    }
</script>

(当然这不是一个非常正确的方法,但是我不能告诉你为什么发生这种与DocumentFragment的调用,也许其他人会发现它)

答案 1 :(得分:1)

我认为问题在于你正在使用event.target。请尝试仅使用$tgt = $(this)var crossBrowserTarget = event.target ? event.target : event.srcElement; $tgt = $(crossBrowserTarget);

答案 2 :(得分:0)

例如:

![在此处输入图片说明] [1]

with“$('。item')。remove();”它不能很好地工作,但使用“$('#bgright')。children()。remove();” 这是因为IE8或者它不支持querySelectorAll。但它在某些特殊情况下得到支持。

enter code here:<section id="bgright">
                      <div class="item"></div>
                      <div class="item"></div>
                </section>
相关问题