2012-09-18 37 views
1

所以我有一个浮动div,我想用jQuery替换内容。例如,在这里我将Bing和Google徽标放入,但尽管检查了文档,似乎无法弄清楚什么是错误的。我注意到其他人遇到了问题,直到他们用自己的标签替换了旧内容,但这一切都没有奏效。问题是因为嵌套的div,还是...?jquery replaceWith没有响应点击呼叫

<style> 
#content { 
    float: right; 
    width: 650px; 
    overflow-x: hidden; 
    color:fff; 
} 

#contentWrapper { 
    position: relative; 
} 

#explainWrapper1 { 
    float:left; 
    width:350px; 
    } 
</style> 

<script> 
$('#amd1').click(function() { 
      $('#content').replaceWith('<img src="http://www.google.com/images/srpr/logo3w.png">'); 
      }); 
      </script> 

... 

<a href="#" id="amd1">Amendment 1</a> 
      <div id="contentWrapper"> 
       <div id="content"> 
     <img src="http://www.smartinsights.com/wp-content/uploads/2012/07/bing-logo.png"> 
    </div> 
<div id="explainWrapper1"> 
</div></div> 
+0

您尝试在内容存在之前绑定事件。稍后使用删除来绑定事件或调用脚本。 – Eru

回答

3

你应该文档准备内把你的代码处理程序:

$(document).ready(function(){ 
    $('#amd1').click(function() { 
     $('#content').replaceWith('<img src="http://www.google.com/images/srpr/logo3w.png">'); 
    }); 
}) 
+1

是的可能是这个嗯':)'+1! –

+0

谢谢。我最初在div下面的页面中有这个,并且没有打扰地在doc.ready中添加它。 –

1

,请复制粘贴完整的HTML bruv,问题似乎别的地方:

工作演示http://jsfiddle.net/3L37W/

代码

$('#amd1').click(function() { 
      $('#content').replaceWith('<img src="http://www.google.com/images/srpr/logo3w.png">'); 
      });​