【问题标题】:Javascript say Uncaught TypeError: Cannot read property 'nodeType' of undefinedJavascript 说 Uncaught TypeError: Cannot read property 'nodeType' of undefined
【发布时间】:2015-12-23 13:12:56
【问题描述】:

我有这个功能:

$( "#contact" ).on( "click", function() {
  var email = $(this).attr(email);
  var sendGmail = function(){
    var str = 'http://mail.google.com/mail/?view=cm&fs=1'+
        '&to=' + to +
        '&su=' + subject +
        '&body=' + message.replace(/\n/g,'%0A') +
        '&ui=1';
    location.href = str;
  };
  sendGmail({
    to: email,
    subject: 'Obavestenje u vezi aukcije ',
    message: 'Postovani, \n'+
    ' \n'+
    'Cestitamo, \n'+
    'Obavestavamo Vas da je Vasa ponuda pobedila na aukciji \n'+
    ' \n'+
    'Uskoro Vas kontaktiramo telefonon \n'+
    ' \n'+
    ' \n'+
    ' \n'+
    ' \n'+
    'Srdacan pozdrav i hvala na ukazanom poverenju. Radujemo se buducoj saradnji\n'
  });


});

我当然有 html:

<button class="btn btn-success btn-sm" id="contact" email="aaa.ddd@gmail.com">Contact user!</button>

但现在当我点击 btton 时出现错误:

未捕获的类型错误:无法读取未定义的属性“nodeType”

我真的不知道我的代码有什么问题?

【问题讨论】:

  • 你的函数中没有nodeType
  • 您给函数一个具有属性的对象,但您的函数不接受任何参数。您可以使用 arguments 变量,但您也不使用它...要么更改您在每个参数中传递的方法,要么添加一个参数并使用它来访问方法中的属性。
  • 好的,谢谢,请写为答案接受正确答案
  • 这不正确:$(this).attr(email),你需要这样做:$(this).attr('email')。您想获取电子邮件属性,但按照您的方式传递一个变量。对于您错误传递的另一个参数,您已经有来自 Taplar 的评论。

标签: javascript jquery function


【解决方案1】:

嗯,这里有一个未定义的变量:

$(this).attr(email);
//-----------^

猜你需要替换它:

$(this).attr("email");

【讨论】:

    猜你喜欢
    • 2017-04-24
    • 1970-01-01
    • 2013-01-15
    • 2019-02-12
    • 2021-03-16
    • 2021-09-20
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多