페이지 헤드에 스크립트가있는 스크립트 태그를 추가하려고하는데 코드를 추가하고 있는데 작동하지 않습니다. 아마도 페이지가 이미 실행 중이기 때문일 것입니다.
어떻게 이런 일을 할 수 있습니까?
$(document).ready(function () {
$('a').on('click', function () {
$('head').append('<script>console.log("running")</script>');
});
});
시험
$(document).ready(function () {
$('a').on('click', function () {
$('<script>').text('console.log("running")').appendTo('head');
});
});
편집하다
위의 예제를 사용하여 함수 호출하기 : DEMO
$(document).ready(function () {
$('a').on('click', function () {
$('<script>').text('test()').appendTo('head');
});
});
function test(){
console.log("running")
}
편집 . 다음과 같이 스크립트 태그를 추가 할 수도 있습니다. DEMO
$("head").append($("<script />", {
text: 'test()',
}))
$("head").append($("<script />", {
html: 'test()',
}))
$("head").append($("<script />", {
src: url,
}))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다