How can I access or get the div id of an element inside a form using an external js file?
Here's the example asp file
<html>
<head>
<script src="jquery.js"></script>
<script src="jsqueryms.js></script>
<script src="myjs.js"></script>
<body onload="initmyjs();">
<form id="formid">
<input type="button" id="button"/>
开发者_如何学Python <div id='dialog'>this is modal dialog</dialog>
</form>
</head>
</html>
external js file
function initmyjs()
{
if document.getelementby('button')!=null
{
document.getelementbyid('button').onlclick=function()
{
$('#dialog').dialog({ modal: true, position: [902, 345], width: 400 });
}
}
}
What I want is to pop a modal dialog once the button clicked, I have no problem accessing it if the script is inside the asp page, but if I put it inside an external js file nothing happened but not script error display. I wonder if the script is correct except that I cannot map the correct element id of my div. Thank you in advance.
Try changing the content of myjs.js to:
$(document).ready(function() {
$('#button').click(function() {
$('#dialog').dialog({ modal: true, position: [902, 345], width: 400 });
});
$('[name="linkComment"]').attr('href', '#').click(function() {
$("#dialog").dialog({ modal: true, position: [902, 345], width: 400 });
});
// Other javascript code that needs to run on page load
});
// Other javascript functions used byt he page
Then get rid of the onload
attribute on the body
tag.
精彩评论