0

我对类和 ajax 有疑问:

嗨,我的页面上有几个 .megavideo div。

  <div class="megavideo">
    <img alt="video" src="https://i1.ytimg.com/vi/6TDQOEATSik/hqdefault.jpg">
    <a href="#nolink" onclick="javascript:play('note', 33);">
      <div class="maxiplay">
        <div class="titremaxiplay">
          CLICK TO PLAY
        </div>
        <img class="play" alt="play" src="kit/play.png">
      </div>
    </a>
  </div>

在 div 内部,有一个带有 javascript 函数的链接。这个javascript函数的目标是刷新链接周围的.megavideo div(只有那个)

我的功能:

function play(what, who){
    $.ajax({
        type: "POST",   
        url: "launchvideo.php",
        data: "what="+what+"&who="+who,
        cache: false,
        success: function(html){
        $(".megavideo").html(html);
      }
    });
}

现在,从逻辑上讲,该函数会刷新每个 .megavideo div。我如何在我的函数上使用 (this) 来仅针对 .megavideo 链接父母?

我在这里找到的最接近的答案是:Get ID of clicked on element in function 但这并不是我想要的。

感谢大家 !托马斯

4

2 回答 2

3

尝试

<div class="megavideo">
    <img alt="video" src="https://i1.ytimg.com/vi/6TDQOEATSik/hqdefault.jpg" /> 
    <a href="#nolink" onclick="play(this, 'note', 33);">
        <div class="maxiplay">
            <div class="titremaxiplay">
                CLICK TO PLAY
            </div>
            <img class="play" alt="play" src="kit/play.png"/>
        </div>
    </a>

</div>

function play(el, what, who) {
    $.ajax({
        type: "POST",
        url: "launchvideo.php",
        data: "what=" + what + "&who=" + who,
        cache: false,
        success: function (html) {
            $(el).closest(".megavideo").html(html);
        }
    });
}
于 2013-10-14T11:27:49.740 回答
1

尝试

HTML

<a href="#nolink" onclick="javascript:play(this,'note', 33);">

JS

function play(that,what, who){
    $.ajax({
        type: "POST",   
        url: "launchvideo.php",
        data: "what="+what+"&who="+who,
        cache: false,
        success: function(html){
        $(that).closest(".megavideo").html(html);
      }
    });
}
于 2013-10-14T11:28:25.883 回答