Javascript 如何处理<;空Div>; 师西恩金姆 肖恩·金 卑尔根县学院的14岁程序员/学生 关于我 我的名字叫肖恩·金,是这所大学的大一新生 在技术和计算机科学学院(ATCS)。 技能 HTML 5 CSS3 PYTHON 联系我 功能sendMail(电子邮件、主题、内容){ $.ajax({ 键入:“POST”, 网址:'https://mandrillapp.com/api/1.0/messages/send.json', 数据:{ “钥匙”:“Goyvulojh3icbemgia ow”, “消息”:{ “来自电子邮件”:电子邮件, “致”:[ { “电子邮件”:seandyk@gmail.com', “姓名”:“肖恩·金”, 'type':'to' } ], “自动图文集”:“真”, “主题”:主题, “html”:内容 } } }).完成(功能(响应){ 如果(响应[0]。状态==“已拒绝”){ document.getElementById(“”.innerHTML=“请输入有效字段”; }否则{ document.getElementById(“”.innerHTML=“已发送!”); } }); }

Javascript 如何处理<;空Div>; 师西恩金姆 肖恩·金 卑尔根县学院的14岁程序员/学生 关于我 我的名字叫肖恩·金,是这所大学的大一新生 在技术和计算机科学学院(ATCS)。 技能 HTML 5 CSS3 PYTHON 联系我 功能sendMail(电子邮件、主题、内容){ $.ajax({ 键入:“POST”, 网址:'https://mandrillapp.com/api/1.0/messages/send.json', 数据:{ “钥匙”:“Goyvulojh3icbemgia ow”, “消息”:{ “来自电子邮件”:电子邮件, “致”:[ { “电子邮件”:seandyk@gmail.com', “姓名”:“肖恩·金”, 'type':'to' } ], “自动图文集”:“真”, “主题”:主题, “html”:内容 } } }).完成(功能(响应){ 如果(响应[0]。状态==“已拒绝”){ document.getElementById(“”.innerHTML=“请输入有效字段”; }否则{ document.getElementById(“”.innerHTML=“已发送!”); } }); },javascript,jquery,html,Javascript,Jquery,Html,我请精通JavaScript的朋友为我编写可以发送电子邮件的代码,因此他使用了Mandrill API并为我编写了一个。但是他告诉我用“空的divs”替换,否则我需要一个span输出到。我不知道那是什么意思。有人能帮忙吗?他说,在html中,您需要创建一个div标记。他基本上是说“嘿,我的代码要做点什么,你需要在你的html页面中创建一个div标记,这样我的代码就可以放进去了。一旦你创建了这个div标记,就转到我给你的代码,用你标记的id替换 然后,在朋友给你的代码中,将替换为id属性(myD

我请精通JavaScript的朋友为我编写可以发送电子邮件的代码,因此他使用了Mandrill API并为我编写了一个。但是他告诉我用“空的
div
s”替换
,否则我需要一个
span
输出到。我不知道那是什么意思。有人能帮忙吗?

他说,在html中,您需要创建一个
div
标记。他基本上是说“嘿,我的代码要做点什么,你需要在你的html页面中创建一个
div
标记,这样我的代码就可以放进去了。一旦你创建了这个
div
标记,就转到我给你的代码,用你标记的
id
替换


然后,在朋友给你的代码中,将
替换为
id属性
myDiv

空div是同时具有打开和关闭,但在打开和关闭之间没有内容或其他元素作为内容的div

您的朋友在代码中提到的Div是:

<!DOCTYPE html>
  <html>
    <head>
      <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> <!-- Bootstrap -->
        <title>Sean Kim</title>
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
          <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
          <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
        <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
        <!--[if lte IE 8]>
        <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/grids-responsive-old-ie-min.css">
        <![endif]-->
        <!--[if gt IE 8]><!-->
        <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/grids-responsive-min.css">
        <!--<![endif]-->
        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
        <link type="text/css" rel="stylesheet" href="css/stylesheet.css">
        <script src="js/jquery-2.1.4.min.js"></script>
    </head>
    <body>
    <div class="custom-menu-wrapper">
    <div class="pure-menu custom-menu custom-menu-top">
      <a href="#" class="pure-menu-heading custom-menu-brand">Sean Kim</a>
      <a href="#" class="custom-menu-toggle" id="toggle"><s class="bar"></s><s class="bar"></s></a>
    </div>
    <div class="me" id="home">
      <br><br><br>
      <h1 class="name">SEAN KIM</h1>
      <hr>
      <h6 class="intro">14 year-old programmer/student at Bergen County Academies</h6>
      <div class="element"></div>
      <img src="images/nyc.jpg" class="img-responsive nyc">
    </div>
    <div id="about">
      <h2 class="about-text">About Me</h2>
      <img src="images/golf.png" align="left"class="propic">
      <p class="info">My name is Sean Kim and I'm a freshman at 
        <a href="http://bcts.bergen.org/index.php/bergen-county-academies" class="bca">
          Bergen County Academies
        </a> in the Academy of Technology and Computer Science (ATCS).
      </p>
    </div>
    <div id="skills">
      <h2 class="skills-text">Skills</h2>
      <div class="pure-g">
        <div class="pure-u-1 pure-u-md-1-4 html5">
          <img src="images/logos/html5.png" alt="HTML 5" width="106" height="150">
          <p class="languages">HTML 5</p>
        </div>
        <div class="pure-u-1 pure-u-md-1-4 css3">
          <img src="images/logos/css.png" alt="CSS3" width="106" height="150">
          <p class="languages">CSS3</p>
        </div>
        <div class="pure-u-1 pure-u-md-1-4 python">
          <img src="images/logos/python.png" alt="PYTHON" width="150" height="150">
          <p class="languages">PYTHON</p>
        </div>
      </div>
    </div>
    </div>
    <div class="contact-container" id="contact">
      <h2 class="contact-text">Contact Me</h2>
      <div class="pure-g contact">
        <div class="pure-u-1 pure-u-md-1-4 Facebook">
          <a href="https://www.facebook.com/seankim327" target="_blank" class="icon">
            <i class="fa fa-facebook-square fa-5x"></i>
          </a>
        </div>
        <div class="pure-u-1 pure-u-md-1-4 Instagram">
          <a href="https://www.instagram.com/seankim.co.kr" target="_blank" class="icon">
            <i class="fa fa-instagram fa-5x"></i>
          </a>
        </div>
        <div class="pure-u-1 pure-u-md-1-4 Twitter">
          <a href="https:/www.twitter.com/sean_d_kim" target="_blank" class="icon">
            <i class="fa fa-twitter-square fa-5x"></i>
          </a>
        </div>
        <div class="pure-u-1 pure-u-md-1-4 GitHub">
          <a href="https://github.com/seankim327" target="_blank" class="icon">
            <i class="fa fa-github-square fa-5x"></i>
          </a>
        </div>
      </div>
      <script>
        function sendMail(email, subject, content) {
          $.ajax({
            type: 'POST',
            url: 'https://mandrillapp.com/api/1.0/messages/send.json',
            data: {
              'key': 'goYVUvLoJH3ICbEMgIA-ow',
              'message': {
                'from_email': email,
                'to': [
                    {
                      'email': 'seandyk@gmail.com',
                      'name': 'Sean Kim',
                      'type': 'to'
                    }
                  ],
                'autotext': 'true',
                'subject': subject,
                'html': content
              }
            }
           }).done(function(response) {
            if (response[0].status == "rejected") {
              document.getElementById("<div id="about"></div>").innerHTML = "Please enter valid fields";
            } else {
              document.getElementById("<div id="skills"></div>").innerHTML = "Sent!";
            }
         });
        }
      </script>
    </div>
  </body>
</html>

简而言之,在HTML
正文中的元素上使用
id
属性,比如“email-status”,在该元素上显示状态更新。使用
document.getElementById(“email-status”)引用它。

这里是一个更详细的解释与更多信息的链接

两者都是元素(默认情况下以换行符与周围文本分隔),或者是元素,都是在Javascript中定期引用和操作的HTML标记

要替换特定元素的内容时,通常使用属性创建选择器。
id
值不能有空格,并且在文档中的所有
id
值中应该是唯一的

例如,一个id为“mail response”的HTML
div
将被简单地写入:

14 year-old programmer/student at Bergen County Academies
在您的示例中,
done
函数的内容如下:

document.getElementById("mail-response").innerHTML = "Entry is <b>invalid</b>";
但是,请注意,
about
skills
元素当前包含内容。替换innerHTML将删除这些元素中当前的内容。根据您的其他注释,与其替换现有块,不如将状态更新插入到初始为空的元素中t已预留用于状态更新


最后,我建议,如果您确实发布了实际的mandrill API密钥,您应该立即更改它。

您可以提供HTML代码吗?您需要在HTML中放置一个带有id的div,如
,然后将该div的id替换为
空div
,并适当注意
id
不会像您的朋友那样包含空格写空Div应该是
EmptyDiv
Empty\u Div
之类的。@GuruprasadRao
不应该是一个ID,而是应该被一个ID替换。我知道@Xufox,但因为他是一个初学者,这是一个建议。我在html代码中的任何地方创建Div吗?(我知道的不多,因为我只编程了大约一年)如果你粘贴html代码,我们当然可以帮你回答。我会先在
中添加它。你可以随时点击F12打开开发工具并进行调试。
<div id="mail-status"></div>
document.getElementById("mail-response").innerHTML = "Entry is <b>invalid</b>";
function(response) {
    if (response[0].status == "rejected") {
        document.getElementById("<div id="about"></div>").innerHTML = "Please enter valid fields";
    } else {
        document.getElementById("<div id="skills"></div>").innerHTML = "Sent!";
    }
}
function(response) {
    if (response[0].status == "rejected") {
        document.getElementById("about").innerHTML = "Please enter valid fields";
    } else {
        document.getElementById("skills").innerHTML = "Sent!";
    }
}