从父页面调用函数时的 JQuery onclick 问题

发布于 2024-11-19 12:39:51 字数 1536 浏览 0 评论 0原文

我遇到了问题..

实际上,我在将值从子页面发送到父页面时确实遇到了问题。 我实际上是通过 JQuery 加载一个页面,并且该页面正在刷新以显示新结果,但这些问题之一是父页面的函数不会从子页面调用。 虽然这在 Google Chrome、Opera 和 Safari 上运行得很好,但在 Firefox 上似乎不起作用。 我听说 Firefox 管理事件的方式与 Safari 或 Google Chrome 不同? 我一直在寻找这个问题的答案,但我找不到任何东西。

好吧,这就是我正在做的:

子页面调用另一个文件,该文件具有使我的网站正常工作的所有功能,这就是应该做的触发函数:

$like = "<a href='$comment_poster' id='$msgid' class='like' 
         onclick='parent.likecomment(this);'>Like</a>";
echo "$like";

这是从位于父页面中的 onclick 事件触发的函数(该函数位于父页面中,onclick 位于子页面中):

该函数是从以下位置接收 id 的函数 :子页面稍后将值添加到 数据库。

function likecomment(commentID)
{
    event.preventDefault();
    var likeid = commentID.id;
    var author = ($(commentID).attr('href'));

    // forming the queryString
    var data = 'likeid='+ likeid + '&author=' + author;
    if(likeid) 
    {
        // ajax call
        $.ajax({
              type: "POST",
              url: "likeprofmessage.php",
              data: data,
              beforeSend: function(html) 
              { 
                  $(".word").html(likeid);
              },
              success: function(html){ 
                  $("#resultsprofcomments").fadeIn('slow');
                  $('#profcommentsdiv').load('showprofmessages.php?vprofile=<?php echo $row['1'];?>').fadeIn("slow");
                  $("#resultsprofcomments").append(html);
              }
        });
    }
}

我尝试过 Firefox 控制台,但收到错误:事件未定义,所以这应该是 Firefox 管理事件的方式问题。 再次感谢您的帮助,非常感谢。

I'm having a problem..

Actually I do have an issue when sending a value from a child page to the parent page.
I'm actually loading a page via JQuery and that page is getting refreshed to display new results, but one of these problems is that the function from the parent page doesn't get called from the child page.
Although this works perfect on Google Chrome, Opera and Safari, it doesn't seem to work on Firefox.
I heard Firefox doesn't manage events the same way as Safari or Google Chrome does?
I've been searching on answers for this but I couldn't find anything pretty much..

Alright, here's what I'm doing:

The child page calls another file which has all the functions that make my site work, this is what should trigger the function:

$like = "<a href='$comment_poster' id='$msgid' class='like' 
         onclick='parent.likecomment(this);'>Like</a>";
echo "$like";

And this is the function that gets fired from the onclick event which is located in the parent page (the function is in the parent page the onclick is in the child page):

This function is the one that receives the id from the child page to later on add the value to the database.

function likecomment(commentID)
{
    event.preventDefault();
    var likeid = commentID.id;
    var author = ($(commentID).attr('href'));

    // forming the queryString
    var data = 'likeid='+ likeid + '&author=' + author;
    if(likeid) 
    {
        // ajax call
        $.ajax({
              type: "POST",
              url: "likeprofmessage.php",
              data: data,
              beforeSend: function(html) 
              { 
                  $(".word").html(likeid);
              },
              success: function(html){ 
                  $("#resultsprofcomments").fadeIn('slow');
                  $('#profcommentsdiv').load('showprofmessages.php?vprofile=<?php echo $row['1'];?>').fadeIn("slow");
                  $("#resultsprofcomments").append(html);
              }
        });
    }
}

I've tried the Firefox console and I've received the error: event is undefined so this should be a problem on how Firefox manages events.
Again, any help is appreciated, thank you very much.

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

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

发布评论

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

评论(3

挽袖吟 2024-11-26 12:39:51

我认为只要您在 HTML 中内联设置 onclick 处理程序(就像您在示例中所做的那样),以下内容就可以工作:

<A href="..." onclick='parent.likecomment(event,this);'>blah</A>

<SCRIPT>

function likecomment(evt, commentID) {
  evt.preventDefault();
  var likeid = commentID.id;
  // etc
}

</SCRIPT>

对于在 HTML 中内联设置的事件处理程序,符合标准的浏览器(包括 FF )应该自动将事件对象作为参数传递给您的处理函数。 IE,直到版本 8,无论如何,使用不同的事件模型并允许您直接引用 event - 实际上它是 window.event 但通常与大多数窗口属性一样它可以工作即使您省略window.

因此,如果您在文档就绪(或加载)时分配点击处理程序,您可以执行以下操作:

function yourOnloadFunction() {
  document.getElementById('yourElementId').onclick = clickHandler;
}

function clickHandler(e) {
  // check if event object was passed in, otherwise use window.event
  if (!e) e = window.event;

  // but how to get a reference to the clicked element?
  // use the event object's target property, except (of course)
  // in IE, which uses srcElement:

  var elRef = e.srcElement ? e.srcElement : e.target;

  // rest of your function here, e.g.
  e.preventDefault();
}

I think the following will work as long as you set the onclick handler inline in your HTML (as you did in your example):

<A href="..." onclick='parent.likecomment(event,this);'>blah</A>

<SCRIPT>

function likecomment(evt, commentID) {
  evt.preventDefault();
  var likeid = commentID.id;
  // etc
}

</SCRIPT>

For event handlers not set inline in HTML, the standards compliant browsers (including FF) should automatically pass the event object as a parameter to your handler function. IE, up to version 8, anyway, uses a different event model and lets you reference event directly - really it is window.event but generally as with most window properties it works even if you omit window..

So if you assign your click handler on document ready (or onload) you can do this:

function yourOnloadFunction() {
  document.getElementById('yourElementId').onclick = clickHandler;
}

function clickHandler(e) {
  // check if event object was passed in, otherwise use window.event
  if (!e) e = window.event;

  // but how to get a reference to the clicked element?
  // use the event object's target property, except (of course)
  // in IE, which uses srcElement:

  var elRef = e.srcElement ? e.srcElement : e.target;

  // rest of your function here, e.g.
  e.preventDefault();
}
夜血缘 2024-11-26 12:39:51

您已设法将代码搞得一团糟,但尝试更改

onclick='parent.likecomment(this);'

onclick='parent.likecomment(this);return false;'

,然后将其删除。

event.preventDefault();

likecomment(commentID) 函数

You have managed to make a mess of your code, but try to change

onclick='parent.likecomment(this);'

to

onclick='parent.likecomment(this);return false;'

and then remove

event.preventDefault();

from your function likecomment(commentID).

谢绝鈎搭 2024-11-26 12:39:51

您是否尝试过发回一个执行类似操作的脚本,而不是使用内联 onClick

<Script>$('#{id}').click(function() { 
    var id = this.id;
    likecomment(id);
    return false;
})</Script>

Instead of using the in-line onClick, have you tried sending back a script that does something like

<Script>$('#{id}').click(function() { 
    var id = this.id;
    likecomment(id);
    return false;
})</Script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文