创建元素和 insertBefore 不起作用

发布于 2024-08-29 23:11:40 字数 3184 浏览 10 评论 0原文

好吧,我一直在用头撞墙,但我不知道为什么它没有创建这个元素。也许我在这里忽略了一些很小的东西。基本上,在正在输出的 PHP 文档中存在这样的 Javascript 代码,就像页面加载时中间的某个位置一样,现在,不幸的是它无法进入标头。虽然我不确定这就是问题所在,但也许是……嗯嗯。

// Setting the variables needed to be set.
    echo '
        <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/shoutbox.js"></script>';
    echo '
        <script type="text/javascript">
            var refreshRate = ', $params['refresh_rate'], ';
            createEventListener(window);
            window.addEventListener("load", loadShouts, false);

            function loadShouts()
            {
                var alldivs = document.getElementsByTagName(\'div\');
                var shoutCount = 0;
                var divName = "undefined";

                for (var i = 0; i<alldivs.length; i++)
                {
                    var is_counted = 0;
                    divName = alldivs[i].getAttribute(\'name\');

                    if (divName.indexOf(\'dp_Reserved_Shoutbox\') < 0 && divName.indexOf(\'dp_Reserved_Counted\') < 0)
                        continue;
                    else if(divName == "undefined") 
                        continue;
                    else
                    {
                        if (divName.indexOf(\'dp_Reserved_Counted\') == 0)
                        {
                            is_counted = 0;
                            shoutCount++;
                            continue;
                        }
                        else
                        {
                            shoutCount++;
                            is_counted = 1;
                        }
                    }

                    // Empty out the name attr.
                    alldivs[i].name = \'dp_Reserved_Counted\';

                    var shoutId = \'shoutbox_area\' + shoutCount;

                    // Build the div to be inserted.
                    var shoutHolder = document.createElement(\'div\');
                    shoutHolder.setAttribute(\'id\', [shoutId]);
                    shoutHolder.setAttribute(\'class\', \'dp_control_flow\');
                    shoutHolder.style.cssText = \'padding-right: 6px;\';
                    alldivs[i].parentNode.insertBefore(shoutHolder, alldivs[i]);

                    if (is_counted == 1)
                    {
                        startShouts(refreshRate, shoutId);
                        break;
                    }
                }
            }

        </script>';

另外,我确信我在这些函数中链接到的其他函数工作得很好。这里的问题是,在这个函数中,div 根本没有被创建,我不明白为什么?此外,Firefox、FireBug 告诉我变量 divName 未定义,尽管我已尝试在函数中处理此问题,但不确定原因。

无论如何,我需要将创建的 div 元素插入到以下 HTML 之前:

echo '
            <div name="dp_Reserved_Shoutbox" style="padding-bottom: 9px;"></div>';

我在这里使用 name 而不是 id,因为我不想要重复的 id 值,这就是为什么我要更改 name 值并递增,因为这函数可能被调用超过 1 次。例如,如果同一页面上有3个shoutbox(不要问为什么...哈哈),我需要跳过我已经更改为“dp_Reserved_Counted”的其他名称,我相信我做得正确。在任何情况下,如果可以的话,我会将其放入标头中并仅调用一次,但这是不可能的,因为这些已加载并且无法分辨它们是哪一个,因此它直接硬编码到实际中输出到Shoutbox 在 HTML 中所在的页面上。基本上,不确定这是否是问题所在,但必须有某种解决方法,除非问题出在我上面的代码中... arrg

请帮助我。我真正需要的是第二双眼睛来关注这个问题。 谢谢 :)

Ok, I've been banging my head up against the wall on this and I have no clue why it isn't creating the element. Maybe something very small that I overlooked here. Basically, there is this Javascript code that is in a PHP document being outputted, like somewhere in the middle of when the page gets loaded, NOW, unfortunately it can't go into the header. Though I'm not sure that that is the problem anyways, but perhaps it is... hmmmmm.

// Setting the variables needed to be set.
    echo '
        <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/shoutbox.js"></script>';
    echo '
        <script type="text/javascript">
            var refreshRate = ', $params['refresh_rate'], ';
            createEventListener(window);
            window.addEventListener("load", loadShouts, false);

            function loadShouts()
            {
                var alldivs = document.getElementsByTagName(\'div\');
                var shoutCount = 0;
                var divName = "undefined";

                for (var i = 0; i<alldivs.length; i++)
                {
                    var is_counted = 0;
                    divName = alldivs[i].getAttribute(\'name\');

                    if (divName.indexOf(\'dp_Reserved_Shoutbox\') < 0 && divName.indexOf(\'dp_Reserved_Counted\') < 0)
                        continue;
                    else if(divName == "undefined") 
                        continue;
                    else
                    {
                        if (divName.indexOf(\'dp_Reserved_Counted\') == 0)
                        {
                            is_counted = 0;
                            shoutCount++;
                            continue;
                        }
                        else
                        {
                            shoutCount++;
                            is_counted = 1;
                        }
                    }

                    // Empty out the name attr.
                    alldivs[i].name = \'dp_Reserved_Counted\';

                    var shoutId = \'shoutbox_area\' + shoutCount;

                    // Build the div to be inserted.
                    var shoutHolder = document.createElement(\'div\');
                    shoutHolder.setAttribute(\'id\', [shoutId]);
                    shoutHolder.setAttribute(\'class\', \'dp_control_flow\');
                    shoutHolder.style.cssText = \'padding-right: 6px;\';
                    alldivs[i].parentNode.insertBefore(shoutHolder, alldivs[i]);

                    if (is_counted == 1)
                    {
                        startShouts(refreshRate, shoutId);
                        break;
                    }
                }
            }

        </script>';

Also, I'm sure the other functions that I'm linking to within these functions work just fine. The problem here is that within this function, the div never gets created at all and I can't understand why? Furthermore Firefox, FireBug is telling me that the variable divName is undefined, even though I have attempted to take care of this within the function, though not sure why.

Anyways, I need the created div element to be inserted just before the following HTML:

echo '
            <div name="dp_Reserved_Shoutbox" style="padding-bottom: 9px;"></div>';

I'm using name here instead of id because I don't want duplicate id values which is why I'm changing the name value and incrementing, since this function may be called more than 1 time. For example if there are 3 shoutboxes on the same page (Don't ask why...lol), I need to skip the other names that I already changed to "dp_Reserved_Counted", which I believe I am doing correctly. In any case, if I could I would place this into the header and have it called just once, but this isn't possible as these are loaded and no way of telling which one's they are, so it's directly hard-coded into the actual output on the page of where the shoutbox is within the HTML. Basically, not sure if that is the problem or not, but there must be some sort of work-around, unless the problem is within my code above... arrg

Please help me. Really what I need is a second set of eyes on this.
Thanks :)

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

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

发布评论

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

评论(3

又怨 2024-09-05 23:11:40

当您测试 divName 时,请将条件顺序从这里切换

                divName = alldivs[i].getAttribute(\'name\');

                if (divName.indexOf(\'dp_Reserved_Shoutbox\') < 0 && divName.indexOf(\'dp_Reserved_Counted\') < 0)
                    continue;
                else if(divName == "undefined") 
                    continue;

为:

                var divName = alldivs[i].getAttribute(\'name\');
                if (!divName) // this is sufficient, by the way
                    continue;
                else if (divName.indexOf(\'dp_Reserved_Shoutbox\') < 0 && divName.indexOf(\'dp_Reserved_Counted\') < 0)
                    continue;

问题是,当脚本找到没有名称的 div 时,它会尝试调用非名称的 indexOf 属性。存在值,因此会引发错误。

When you're testing divName, switch the order of your conditions from this

                divName = alldivs[i].getAttribute(\'name\');

                if (divName.indexOf(\'dp_Reserved_Shoutbox\') < 0 && divName.indexOf(\'dp_Reserved_Counted\') < 0)
                    continue;
                else if(divName == "undefined") 
                    continue;

to this:

                var divName = alldivs[i].getAttribute(\'name\');
                if (!divName) // this is sufficient, by the way
                    continue;
                else if (divName.indexOf(\'dp_Reserved_Shoutbox\') < 0 && divName.indexOf(\'dp_Reserved_Counted\') < 0)
                    continue;

The problem is that when the script finds a div without a name, it tries to call the indexOf property of a non-existent value and therefore throws an error.

天涯离梦残月幽梦 2024-09-05 23:11:40

loadShouts 方法存在许多问题。首先是比较字符串“未定义”而不是直接布尔检查,这将匹配。我还删除了一堆不需要的逻辑。除此之外,分配给新的shoutHolder的id属性是作为数组传入的,而不是直接的属性分配。看看下面的方法是否效果更好。

function loadShouts()
{
  var alldivs = document.getElementsByTagName("div");
  var shoutCount = 0;
  var divName = "undefined";

  for (var i = 0; i<alldivs.length; i++)
  {
    divName = alldivs[i].getAttribute("name");

    if (!divName)
      continue;
    if (divName.indexOf("dp_Reserved_Shoutbox") < 0 && divName.indexOf("dp_Reserved_Counted") < 0)
      continue;

    shoutCount++;    
    if (divName.indexOf("dp_Reserved_Counted") == 0)
      continue;

    // Empty out the name attr.
    alldivs[i].setAttribute("name", "dp_Reserved_Counted");

    var shoutId = "shoutbox_area" + shoutCount;

    // Build the div to be inserted.
    var shoutHolder = document.createElement("div");
    shoutHolder.setAttribute("id", shoutId);
    shoutHolder.setAttribute("class", "dp_control_flow");
    shoutHolder.style.cssText = "padding-right: 6px;";
    alldivs[i].parentNode.insertBefore(shoutHolder, alldivs[i]);

    startShouts(refreshRate, shoutId);
    break;
  }
}

There were a number of issues in the loadShouts method. First being the comparison of a string "undefined" instead of a straight boolean check, which will match. I also removed a bunch of un-needed logic. Beyond this, the id attribute being assigned to the new shoutHolder was being passed in as an array, instead of a direct property assignment.. See if the following works better.

function loadShouts()
{
  var alldivs = document.getElementsByTagName("div");
  var shoutCount = 0;
  var divName = "undefined";

  for (var i = 0; i<alldivs.length; i++)
  {
    divName = alldivs[i].getAttribute("name");

    if (!divName)
      continue;
    if (divName.indexOf("dp_Reserved_Shoutbox") < 0 && divName.indexOf("dp_Reserved_Counted") < 0)
      continue;

    shoutCount++;    
    if (divName.indexOf("dp_Reserved_Counted") == 0)
      continue;

    // Empty out the name attr.
    alldivs[i].setAttribute("name", "dp_Reserved_Counted");

    var shoutId = "shoutbox_area" + shoutCount;

    // Build the div to be inserted.
    var shoutHolder = document.createElement("div");
    shoutHolder.setAttribute("id", shoutId);
    shoutHolder.setAttribute("class", "dp_control_flow");
    shoutHolder.style.cssText = "padding-right: 6px;";
    alldivs[i].parentNode.insertBefore(shoutHolder, alldivs[i]);

    startShouts(refreshRate, shoutId);
    break;
  }
}
隐诗 2024-09-05 23:11:40

好吧,只是想让你知道事情进展如何。我非常感谢 Tracker1 和 Casey Hope。尤其是Tracker的功能进行了出色的重写。你们都很摇滚。顺便说一句,这是我使用的最后一个函数,只需对 Tracker1 的答案进行一点点编辑,这就是为什么你轻易地获得了我的投票!

echo '
    <script type="text/javascript">
        var refreshRate = ' . $params['refresh_rate'] . ';
        createEventListener(window);
        window.addEventListener("load", loadShouts, false);

        function loadShouts()
        {
          var alldivs = document.getElementsByTagName("div");
          var shoutCount = 0;
          var divName = "undefined";

          for (var i = 0; i<alldivs.length; i++)
          {
            divName = alldivs[i].getAttribute("name");

            if (!divName)
              continue;
            if (divName.indexOf("dp_Reserved_Shoutbox") < 0 && divName.indexOf("dp_Reserved_Counted") < 0)
              continue;

            shoutCount++;    
            if (divName.indexOf("dp_Reserved_Counted") == 0)
              continue;

            // Empty out the name attr.
            alldivs[i].setAttribute("name", "dp_Reserved_Counted");

            var shoutId = "shoutbox_area" + shoutCount;

            // Build the div to be inserted.
            var shoutHolder = document.createElement("div");
            shoutHolder.setAttribute("id", shoutId);
            shoutHolder.setAttribute("class", "dp_control_flow");
            shoutHolder.style.cssText = "padding-right: 6px;";
            alldivs[i].parentNode.insertBefore(shoutHolder, alldivs[i]);

            startShouts(refreshRate, shoutId);
            break;
          }
        }

    </script>';

再次感谢,你是最棒的!

Ok, just wanted to let you know how it went. And I thank both you greatly Tracker1 and Casey Hope. Especially Tracker for the excellent rewrite of the function. You all ROCK. Here's the final function that I'm using bytheway, just a tiny bit of editing to Tracker1's Answer, which is why you got my vote hands down!

echo '
    <script type="text/javascript">
        var refreshRate = ' . $params['refresh_rate'] . ';
        createEventListener(window);
        window.addEventListener("load", loadShouts, false);

        function loadShouts()
        {
          var alldivs = document.getElementsByTagName("div");
          var shoutCount = 0;
          var divName = "undefined";

          for (var i = 0; i<alldivs.length; i++)
          {
            divName = alldivs[i].getAttribute("name");

            if (!divName)
              continue;
            if (divName.indexOf("dp_Reserved_Shoutbox") < 0 && divName.indexOf("dp_Reserved_Counted") < 0)
              continue;

            shoutCount++;    
            if (divName.indexOf("dp_Reserved_Counted") == 0)
              continue;

            // Empty out the name attr.
            alldivs[i].setAttribute("name", "dp_Reserved_Counted");

            var shoutId = "shoutbox_area" + shoutCount;

            // Build the div to be inserted.
            var shoutHolder = document.createElement("div");
            shoutHolder.setAttribute("id", shoutId);
            shoutHolder.setAttribute("class", "dp_control_flow");
            shoutHolder.style.cssText = "padding-right: 6px;";
            alldivs[i].parentNode.insertBefore(shoutHolder, alldivs[i]);

            startShouts(refreshRate, shoutId);
            break;
          }
        }

    </script>';

Thanks Again, you are the BEST!

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