用Javascript调用函数

Calling a Function in Javascript

本文关键字:函数 调用 Javascript      更新时间:2023-09-26

我已经声明了一个函数,用于在jQuery 中显示对话框

<script type="text/javascript">
function showDialog(str,strtitle)
 {
if(!strtitle) strtitle='Error message';
$('#dialog').dialog('destroy');
$('#dialog').show();
$('#dialog').html(str);
$("#dialog").dialog({
    resizable: false,
    width: 400,
    color: '#BF5E04',
      open: function () {
                    $(this).parents(".ui-dialog:first").find(".ui-dialog
                                titlebar").addClass("ui-state-error");},
    buttons: {"Ok": function() { 
    $(this).dialog("close");    }},
    overlay: { opacity: 0.2, background: "cyan" },title:strtitle});}
   </script>

我在另一个javascript代码中调用这个函数:

   <script type="text/javascript">
    var myFile = document.getElementById('myfile');
    //binds to onchange event of the input field
    myFile.addEventListener('change', function() {
    //this.files[0].size gets the size of your file.
     var  size = this.files[0].size;
     document.write(showDialog('size','File Size Exceeds')); 
      });
     </script>

当我执行该函数时,它会写"未定义,为什么对话框没有显示"。第一个功能是头部倾斜,第二个功能是身体部分倾斜。

document.write()正在写入showDialog()返回的内容。由于该函数没有返回任何内容,因此它将写入Undefined

document.write()不是必需的,只需调用showDialog()即可。

showDialog()函数中没有返回任何值。

只要去掉document.write()showDialog()就不会返回任何内容,因此会出现未定义的错误。

<script type="text/javascript">  
  var myFile = document.getElementById('myfile');     
  //binds to onchange event of the input field     
  myFile.addEventListener('change', function() {     
  //this.files[0].size gets the size of your file.      
    var  size = this.files[0].size;      
    showDialog('size','File Size Exceeds');        
  });     
</script>