<a href="#" onclick="passid(idvalue)" class="generate">Generate ID</a>
上面是我的PHP代码,我在JavaScript函数中传递id值
function passid(id)
{
var gener = "<script type=\"text/javascript\">
var id = "+id+"<\/script>
<script src=\"http://mySample.com/MYjs/myJS.js\"><\/script>";
document.getElementById('codegen').textContent = gener ;
}
从此我想在myJS.js
文件中传递ID如何发送此ID。
答案 0 :(得分:0)
为什么不使用Jquery?
$('.generate').on('click', function () {
passid(1);
});
function passid(id) {
var gener = "<script type=\"text/javascript\">var id = "+id+";alert(id);<\/script>"+"<script>alert(id);<\/script>"+"<script src=\"alert.js\"><\/script>";//Alert inside JS file too!
$(gener).appendTo(document.body);
}
http://jsfiddle.net/LeroyRon/bk9fryuj/
///没有jQuery版本 环顾四周:Adding <script> element to the DOM and have the javascript run?
答案 1 :(得分:-1)
只需创建id全局变量并更改html,您需要使用innerHTML:
function passid(id) {
window.id = id;
var gener = "<script src=\"http://mySample.com/MYjs/myJS.js\"><\/script>";
document.getElementById('codegen').innerHTML = gener;
}