2012-05-22 91 views
-1

这件事情很明显,但我不能看到它虽然看着其他人都面临着,并得到在计算器解决了非常类似的问题。我希望它跳出你的身体。请帮我弄清楚为什么我无法插入(或插入后)现有的元素。的jQuery/insertAfter - 无法插入一个新的元素

我包括我的HTML视图文件(我用笨)与相关的JavaScript。

与以往一样,非常感谢。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <title>Afterview</title> 
    <script src="jquery-1.6.4.js" type="text/javascript" charset="utf-8"></script> 
    </head> 
    <body> 
     <div class="[email protected]"> 
      <li> 
       Click for more details.&nbsp; 
       <a id="[email protected]" class="detailbtn" href="#detail-id">(Details)</a> 
      </li> 
     </div> 
    </body> 
</html> 

<script type="text/javascript"> 
    var html_string; 
    $('.detailbtn').each(function(i){ 
     $(this).click(function(){ 
      html_string='<li>hello</li>'; 
      $('[email protected]').insertAfter(html_string).show(); 
     }); 
    }); 

</script> 

回答

1

我想你想改变你的代码,使用.after()和修复你的类名是这样的:

$('.divmncom').after('<li>hello</li>').show(); 

类名称更改为<div class="divmncom">摆脱正在不同的解释特殊字符比你想要的。

而且,当你清理东西,你应该摆脱html_string全局变量,方法是彻底消除了需要它或使它的局部变量。


作为解释,看起来你不明白insertAfter()如何工作。当你有:

$(x).insertAfter(y) 

其插入的每个,选择器xy表示的目标后匹配元件。在您的代码:

html_string='<li>hello</li>'; 
$('[email protected]').insertAfter(html_string).show(); 

html_string不匹配任何现有的DOM元素,所以它没有一个地方做的插入。

此外,CSS选择器[email protected]'可能与@性格的问题,将在.com解释.为第二类,这是不是你想要的开始。有很多方法可以转义这些特殊字符并仍然使用它们,但是更安全的代码是完全停止使用它们,并且使您的类名称变为简单的类名称,而不需要特殊字符转义。

你或许应该阅读这两个相关的jQuery方法.after().insertAfter()的网页,以确保应用正确的参数,以正确的方法为您的特定问题。它看起来像你想用.after()来代替。

1

我不明白的代码,但你一定要逃逸,从它的外观选择

$('.divm\\@n\\.com') 

编辑

周期和@可以用

替代

$('.detailbtn').each(function(i){   
     $(this).click(function(){ 
      html_string='<li>hello</li>'; 
      $('[email protected]').insertAfter(html_string).show(); 
     }); 
    }); 

$(".detailbtn").click(function(e){ 
    e.preventDefault(); 
$("<li>hello</li>").insertAfter(".divm\\@n\\.com"); 
}); 

DEMO

P.S.请参阅@ jfriend00 answer insertAfter如何工作

+0

谢谢你的解释。我也读了一些这方面的内容。非常感激。 – user1072910

0

类名“[email protected]”无效,因为它包含'。',这意味着该元素应该有一个类“divm @ n”和一个“com”类。 并从这个元素之后html_string插入值,你必须使用的。经过()函数或更改当前.insertAfter()这样的:

$(html_string).insertAfter('[email protected]').show(); 

这是,我得到了什么:

var html_string; 
$('.detailbtn').each(function(i){ 
    $(this).click(function(){ 
     html_string='<li>hello</li>'; 
     $('[email protected]').after(html_string).show(); 
    }); 
});​ 
+0

谢谢!这非常有帮助。我需要重新思考当前需要.com部分的一些逻辑。再次感谢。 – user1072910

1

其实你需要2个转义变量,一个用于@,另一个用于.

对于我明白了,看来要附加一些HTML代码链接被点击之后,如果是这样,我建议使用append()(你也可以使用after()如果你想)

HTML

<div class="[email protected]"> 
<li> 
    Click for more details.&nbsp; 
    <a id="[email protected]" class="detailbtn" href="#detail-id">(Details)</a> 
</li>  

JS

$('.detailbtn').click(function(){ 
    var html_string='<li>hello</li>'; 
    $('.divm\\@n\\.com').after(html_string); 
}); 

Here是一个可行的解决方案...

+0

非常感谢!我会研究这种方法 - 特别感谢工作中的Soln!非常感激 – user1072910