2009-06-07 7 views
12

항목을 확장 할 때 JavaScript을 추가하려면 jQuery을 사용하고 있습니다.jQuery - DivX에 자바 스크립트 추가

하지만 jQuery 함수 내부의 JavaScript 코드로 인해 나머지 jQuery 코드가 폭발하고 있습니다.

$(this).parent('li').find('.moreInfo').append('<script>alert("hello");</script>'); 

나는이 문제를 해결하기 위해 무엇을 할 수 있는가? 더 나은 옵션 와이어 업에 리튬의 클릭 이벤트가 될 것입니다 그렇지 않으면

var str="<script>alert('hello');"; 
str+="<"; 
str+="/script>"; 

$(this).parent('li').find('.moreInfo').append(str); 

: 다음 추가 자바 스크립트 문제의 사용이 들어

+0

처럼 사용해 볼 수 있습니까? – TheVillageIdiot

+0

HTML은 여기에 지나치게 적용되지 않으며 자바 스크립트의 목적은 원격 소스에서 정보를 가져 오는 것입니다. 하지만로드시 내 사이트의 70 개 항목에 대해이 정보를 가져 오는 대신 요청시 가져 오는 것이 좋습니다. – Tim

+0

하지만 왜 div에 javascript를 추가 하시겠습니까? 클릭시 노드의 내용을 가져 와서 div 또는 div에 추가 할 수 있습니다. – TheVillageIdiot

답변

17

$("ul#myList li").click(function(){ 
     $.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      url: "url to your content page", 
      data: "{}",//any data that you want to send to your page/service 
      dataType: "json", 
      success: function(msg) { 
       $(this).find('.moreInfo').append(msg); 
      } 
     }); 
}); 

가 사실은 내가 모르는 어떤 언어 당신이 사용하고 있습니다. ASP.NET을 사용하는 경우 Dave의 blog을 읽어주세요. \/script 전에이 문제를 해결해야 추가

2
$(document).ready(function() { 
     $("ul").each(function() { 
      var _list = this; 
      $(_list).find("li").click(function() { 
       var _listItem = this; 
       $(this).animate({ height: 20 }, 300); 
       $(this).append("<script type='text/javascript'>" + "$('.resultsParag').text('I am injected via JS and will not mess up the other JS you have written cause I am a good boy');" + "</" + "script>"); 
      }); 
     }); 
    }); 
0

는 HTML을 게시하고 스크립트를 추가의 목적을 알려주십시오 것이

$(this).parent('li').find('.moreInfo').append('<script>alert("hello");</' + 'script>'); 
관련 문제