jQuery警报说未定义 [英] jquery alert says undefined

查看:80
本文介绍了jQuery警报说未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用alert方法来显示值c8,但它只是未定义。

Hi, I'm trying to get the alert method to show the value c8 but it just says undefined.

我是html和jquery的新手;很简单,但我看不到。

I'm new to html and jquery; it's simple but I just can't see it.

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="jquery/jquery-1.7.1.js"></script>
    <script>
      $(document).ready(function(){
        $("button").click(function(){
      //$("#div1").load("hs.php");
      //$("#div1").load("http://127.0.0.1:81/tenHsServer/tenHsServer.aspx?t=ab&f=DeviceStatus&d=A1");
        $("#div1").load("http://127.0.0.1:81/tenHsServer/tenHsServer.aspx?t=ab&f=ToggleDevice&d=c8");
        alert($(this).prev().attr(".class"));
       });
      });
    </script>
  </head>
  <body>
    <div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
    <span class='c8'><button>Get External Content</button></span>
  </body>
</html>


推荐答案

按钮没有兄弟姐妹 .prev()用于选择上一个同级,要定位的元素是其父元素,因此使用 .parent()进行选择。另外,如果类是选择器的一部分,则只在类前面加点()。

The button has no siblings .prev() is used to select the previous sibling, the element you want to target is its parent therefore use .parent() to select it. Also you only prepend the class with a dot (.) when it is part of a selector.

alert($(this).parent().attr("class"));

这篇关于jQuery警报说未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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