Jquery查找和替换内容的问题

时间:2018-07-26 16:02:53

标签: jquery html css

伙计们,   处理一个简单的问题,即用类“ span”为每个reason替换屏幕上的文本。我已经尝试了加载和就绪事件,但没有任何运气。我的页面看起来像这样:

<div class="sp-body">
   <div>......</div>
   <div>......</div>
   <section>......</section>
   <div>
        <span class="reason ng-binding">Trying To Replace This Reason!!!</span>

   </div>
     .
     .
     .
     .

到目前为止,我已经尝试过:
方法1:

jQuery(window).load(function(){
    //alert("JQuery Version: " + jQuery.fn.jquery);  //This alert works
    jQuery(".reason").each(function() {
        console.log("Found span class");
           $(this).text("Replacing with this!!!!");
         });
    console.log("After.......................");
});

方法2:

jQuery(document).ready(function(){    
    //alert("JQuery Version: " + jQuery.fn.jquery);  //This alert works
    jQuery(".reason").each(function() {
        console.log("Found span class");
           $(this).text("Replacing with this!!!!");
         });
    console.log("After.......................");
});

PS:页面是使用JSF和AngularJS生成的(我认为这对生成的HTML无关紧要)

1 个答案:

答案 0 :(得分:0)

对我来说很好!确切的问题是什么?

$("span.reason").each(function(){
  
  $(this).text("Replacing with this !");
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sp-body">
   <div>......</div>
   <div>......</div>
   <section>......</section>
   <div>
        <span class="reason ng-binding">Trying To Replace This Reason!!!</span>

   </div>
   <div>
        <span class="reason ng-binding">Trying To Replace This Reason!!!</span>

   </div>
   <div>
        <span class="reason ng-binding">Trying To Replace This Reason!!!</span>

   </div>
   <div>
        <span class="reason ng-binding">Trying To Replace This Reason!!!</span>

   </div>