2012-05-22 32 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