如何在aspx中调用javascript函数 [英] how to call javascript function in aspx

查看:90
本文介绍了如何在aspx中调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是HTML javascript无法在aspx页面中运行,但我想在aspx中运行代码。





我的HTML代码为:



My probelm is HTML javascript is not working in an aspx page, but I want to run code in aspx.


My HTML code is:

<html>
<head>
    <title></title>
</head>
<body>

<div style="text-align:center"> 
  <button  önclick="playPause()">Play/Pause</button> 
  <button  önclick="makeBig()">Big</button>
  <button  önclick="makeSmall()">Small</button>
 <button  önclick="makeNormal()">Normal</button>
 <input type="text"  id="t1"   önblur="setTime()"/>
  <select name="hai" id="hai1" >
  <option value="0.5">0.5kbps</option>
  <option value="1.0">1.0kbps</option>
  <option value="1.5">1.5kbps</option>
  <option value="2.0">2.0kbps</option>
   </select>
   <button  önclick="bitrates()" >play</button>
  <br> 
  <video id="video1" width="420" controls="controls" src="video/ram.MP4">
  </video>
</br></div> 

<script type="text/javascript">
    var myVideo = document.getElementById("video1");


    function playPause() {
        if (myVideo.paused)
            myVideo.play();

        else
            myVideo.pause();
    }

    function makeBig() {
        myVideo.width = 600;
    }

    function makeSmall() {
        myVideo.width = 320;
    }

    function makeNormal() {
        myVideo.width = 420;
    }
    function bitrates() {

        myVideo.playbackRate = document.getElementById("hai1").value;
    }

    function setTime() {

        myVideo.currentTime = document.getElementById("t1").value;
    }


   
</script> 

</body>
</html>





请帮帮我,如何在aspx文件中使用javascript。



Please help me, how to use javascript in aspx file.

推荐答案

这只是因为你的脚本是没有做任何可以表现为工作的事情。让我们看看:首先,它正确地获得了对DOM元素video的引用。然后你创建6个函数。你只需创建7个对象,没有别的。之后,脚本将超出其范围,最终,所有这7个对象将被垃圾收集。就这样。问题是:没有调用任何函数。你应该在脚本文本的末尾至少调用一个,它应该引导所有的功能。



通常,脚本是你的<$的孩子c $ c>< body> 元素用于将事件处理程序添加到某些DOM元素的某些事件中。即使在您的脚本完成并且超出其范围之后,添加到这些HTML元素的某些事件的事件处理程序的元素将保留在浏览器的内存中,直到您关闭整个页面。这意味着您的处理程序功能也将保留在浏览器的内存中,并将响应用户触发的事件。这就是它的全部工作原理。



如果是视频,基于你的代码的最小脚本将会起作用:

This is just because your script is not doing anything which could be manifested as "work". Let's see: first, it obtains the reference to the DOM element "video", correctly. Then you create 6 functions. You simply create 7 objects, nothing else. After that, the scripts runs out of its scope, and eventually, all those 7 objects will be garbage-collected. That's all. The problem is: none of your functions is called. You should call at least one, at the end of your script text, and it should bootstrap all the functionality.

Usually, the script being a child of your <body> element is used to add event handlers to some events of some DOM elements. Even after your scrip is finished and runs out of its scope, the elements with event handlers added to some events of those HTML elements will stay in the browser's memory until you close the whole page. It means that your handler functions, too, will be kept in browser's memory and will respond to events triggered by the user. This is how it all works.

In case of video, the minimal script based on your code will work if you do this:
<script type="text/javascript">

    myVideo = document.getElementById("video1");

    function playPause() {
        if (myVideo.paused)
            myVideo.play();
        else
            myVideo.pause();
    }

    playPause();

</script>





注意最后一行,这就是它的工作原理。



祝你好运,

-SA


这篇关于如何在aspx中调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆