iframe과 버튼이있는 html 파일이 있습니다. 버튼을 클릭 한 후 iframe의 본문 안에 javascript 함수를 추가 할 수 있습니까? 여기 내 코드가있다. 감사합니다iframe의 본체 안에 javascript 함수를 추가하는 방법
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script>
function callMe() {
var frame = $('iframe'),
contents = frame.contents(),
body = contents.find('body');
var script2 = document.createElement("script");
script2.type = "text/javascript";
script2.text = " function setEmployeeId(){var employeeId = 0;};"
$(body).append(script2);
};
</script>
<title>sample</title>
</head>
<body>
<iframe src="page2.html">
</iframe>
<button onClick="callMe();">click</button>
</body>
</html>
내가 원하는 결과는 이와 같아야합니다.
<html>
<head>
<title>sample</title>
</head>
<body>
<iframe>
<html>
<head>
<title></title>
</head>
<body>
<script>
function setemployeeId() {
var employeeId = 0;
};
</script>
</body>
</html>
</iframe>
</body>
</html>
도메인의 프레임 페이지가 있습니까? –
@AndrewBarber 그래서'src'는 상대적인 "page2.html"이라고 생각합니다. – zamnuts
여기에서 답을 찾을 수 있습니다 ... http://stackoverflow.com/questions/1591135/why-does-appending-a-script-to-a-dynamically-created-iframe-seem-to-run- – gts101