通过id获取元素问题?

发布于 2024-12-28 10:01:49 字数 1586 浏览 2 评论 0原文

我试图通过使用带有 onclick 命令的图像来淡入一个框。但我不知道为什么它不起作用。

任何帮助将不胜感激!这是代码:

<style>
  article, aside, figure, footer, header, hgroup,
  menu, nav, section { display: block; }
   #box { background:#999; width:500px; height:500px;opacity:0; }
</style>
</head>

  <div id="box"><id="box"></div>
  <div id="img"><img src="img.png" width="37" height="28" id="img"></div>


<script>
    var elem = document.getElementById("img","box");
    // attach event handler
   "img".onclick = function(){
      fadeIn( "img", 400 );
      this.onclick = null;
    };

    function setOpacity( obj, value ) {
      if ( obj ) {
        obj.style.opacity = value / 100;
        obj.style.filter  = 'alpha(opacity=' + value + ')';
        obj.style.zoom    = 1;
      }
    }

    // makes an element to fade in
    function fadeIn( dom, interval, delay ) {

          interval  = interval || 1000;
          delay     = delay    || 10;

      var opacity   = 0,
          start     = Number(new Date()),
          op_per_ms = 100 / interval;

      if ( typeof dom === "string" ) {
        dom = document.getElementById( dom );
      }

      function step() {

        var now     = Number(new Date()),
            elapsed = now - start;
            opacity = elapsed * op_per_ms;

        setOpacity( dom, opacity );

        if ( elapsed < interval )
          setTimeout( step, delay );
        else
          setOpacity( dom, 100 );
      }

      setTimeout( step, delay );
    }

</script>

I am trying to fade a box in by using an image with a onclick command. But I am lost as to why it's not working.

Any help would be greatly appreciated! Here is the code:

<style>
  article, aside, figure, footer, header, hgroup,
  menu, nav, section { display: block; }
   #box { background:#999; width:500px; height:500px;opacity:0; }
</style>
</head>

  <div id="box"><id="box"></div>
  <div id="img"><img src="img.png" width="37" height="28" id="img"></div>


<script>
    var elem = document.getElementById("img","box");
    // attach event handler
   "img".onclick = function(){
      fadeIn( "img", 400 );
      this.onclick = null;
    };

    function setOpacity( obj, value ) {
      if ( obj ) {
        obj.style.opacity = value / 100;
        obj.style.filter  = 'alpha(opacity=' + value + ')';
        obj.style.zoom    = 1;
      }
    }

    // makes an element to fade in
    function fadeIn( dom, interval, delay ) {

          interval  = interval || 1000;
          delay     = delay    || 10;

      var opacity   = 0,
          start     = Number(new Date()),
          op_per_ms = 100 / interval;

      if ( typeof dom === "string" ) {
        dom = document.getElementById( dom );
      }

      function step() {

        var now     = Number(new Date()),
            elapsed = now - start;
            opacity = elapsed * op_per_ms;

        setOpacity( dom, opacity );

        if ( elapsed < interval )
          setTimeout( step, delay );
        else
          setOpacity( dom, 100 );
      }

      setTimeout( step, delay );
    }

</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏拉图鍀咏恒 2025-01-04 10:01:49

getElementById 没有超过 1 个参数

var elem = document.getElementById("img");
var elem2 = document.getElementById("box");

此外,您的 IE 事件处理程序正在从字符串调用方法。

elem.onclick = function(){
  fadeIn(this, 400 );
  return false;
};

getElementById does not have more than 1 argument

var elem = document.getElementById("img");
var elem2 = document.getElementById("box");

Also your IE event handler is calling a method from a String.

elem.onclick = function(){
  fadeIn(this, 400 );
  return false;
};
少女的英雄梦 2025-01-04 10:01:49

我认为您在第一段代码中犯了两个错误:

<script>
    var elem = document.getElementById("img","box");
    // attach event handler
   "img".onclick = function(){
      fadeIn( "img", 400 );
      this.onclick = null;
    };

首先:

var elem = document.getElementById("img","box");

是错误的,因为函数 getElementById 仅采用一个参数。如果你想获取两个元素,你应该使用两个不同的调用:

var img = document.getElementById("img");
var box = document.getElementById("box");

其次,这是什么?

"img".onclick = function(){

我认为你的意思是这样的:

elem.onclick = function(){

第一次更正后变成:

    img.onclick = function(){
      fadeIn( "img", 400 );
      this.onclick = null;
    };

    box.onclick = function(){
      fadeIn( "box", 400 );
      this.onclick = null;
    };

I think you're making two errors within this first piece of code:

<script>
    var elem = document.getElementById("img","box");
    // attach event handler
   "img".onclick = function(){
      fadeIn( "img", 400 );
      this.onclick = null;
    };

First of all:

var elem = document.getElementById("img","box");

is wrong because the function getElementById just takes one parameter. If you want to get two elements, you should use two different calls:

var img = document.getElementById("img");
var box = document.getElementById("box");

Secondly what's this?

"img".onclick = function(){

I think you meant this:

elem.onclick = function(){

That with the first correction becomes:

    img.onclick = function(){
      fadeIn( "img", 400 );
      this.onclick = null;
    };

    box.onclick = function(){
      fadeIn( "box", 400 );
      this.onclick = null;
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文