简体   繁体   English

onclick执行javascript函数并在div中返回结果

[英]onclick execute javascript function and return results in a div

First off, let me apologise if this is a duplicate but none of my searches left me any more enlightened than I am currently. 首先,如果这是重复的操作,请允许我道歉,但是我的搜索都没有比现在更能启发我了。 That said, I am currently trying my hand at JavaScript/Ajax/jQuery and I have hit a slight snag. 也就是说,我目前正在尝试使用JavaScript / Ajax / jQuery,但遇到了一点障碍。

I want to include a functionality check on my home page where users can test that their browsers are configured to allow JavaScript & cookies (I'm not overly concerned about making the code degradable if a user's browser doesn't allow for JavaScript & cookies as this site is for exclusive use of our company's clients and we, pretty much, dictate the terms and conditions for using our website). 我想在首页上添加功能检查,以便用户可以测试其浏览器是否配置为允许JavaScript和cookie(如果用户的浏览器不允许将JavaScript和cookie用作代码,我不太担心代码是否可降解)本网站仅供我们公司的客户使用,我们几乎规定了使用我们网站的条款和条件。

Now, after a bit of research (GIYF and all that), it appears that the best way to do this would be to let JavaScript set the cookie, thereby killing two birds with one stone. 现在,经过一番研究(GIYF等),看来最好的方法是让JavaScript设置cookie,从而用一块石头杀死两只鸟。 Granted, we won't be able to narrow down the failure to "one or the other" but I am trying to keep this as simple (for me) as possible. 当然,我们无法将失败的范围缩小到“一个或另一个”,但我正在尝试(对我而言)尽可能地简化。 Even so, I am not sure if I am going about this the right way. 即使这样,我不确定我是否会以正确的方式进行操作。 Here is the code from my .js file: 这是我的.js文件中的代码:

function error_reporting (level) {
 // http://kevin.vanzonneveld.net
 // +   original by: Brett Zamir (http://brett-zamir.me)
 return this.ini_set('error_reporting', level);

}

var set = $(document).getUrlParam('set');

if(set !== 'yes') {

  var date = new Date();
  var midnight = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59);

  $.cookie('candcptest', 'ok', {

    expire: midnight,
    path: '/',
    domain: 'candcp.rossdb.net',
    secure: false

  }, function() {

    window.document.reload();

  });

} else {

  var candcptest = $.cookie('candcptest');

  if(candcptest !== undefined) {
    var ccptresult = 'Thank you.  Your browser is configured to allow JavaScript and accept cookies.  You may proceed with login';
  } else {
    var ccptresult = 'Thank you.  Unfortunately, your browser is not configured correctly.  Please check your settings or contact the Dev Team for assistance';
  }
};

(I had previously written a very basic test for allowing cookie in PHP and then modified that to create this so the above may be way off the mark in and of itself) (我之前编写了一个非常基本的测试以允许PHP中使用cookie,然后对其进行了修改以进行创建,因此上述内容本身可能与标准不符)。

Now, ideally, I would like to use jQuery, as this seems like a task well-suited to it but, clearly, I have no idea how to go about this. 现在,理想情况下,我想使用jQuery,因为这似乎很适合它,但是,显然,我不知道该如何做。 This is what I have in my HTML file: 这就是我的HTML文件中的内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="styles/global.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="js/jquery.cookie.js"></script>
    <script type="text/javascript" src="js/jquery.getUrlParam.js"></script>
    <script type="text/javascript">
      $(function() {
        $('a#ccpt').click('js/candcptest',function() {
          $('div#ccptresult').append(result);
        });
      });
    </script>
  </head>

  <body class="body">
    <table width="1000" border="0" cellpadding="0" cellspacing="0" align="center" bgcolor="#FFFFFF">
      <tr>
        <td width="1000" height="545" align="center" valign="middle">
          <table border="0" align="center">
            <tr>
              <td width="750" height="150" align="center" class="text0down">
                This website makes use of JavaScript and cookies and it is essential that your browser is configured correctly in order for this website to work. If you are unsure if your browser is set up correctly, click <a href="#" class="text0down" id="ccpt">here</a> to test.
                <div id="ccptresult"></div>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </body>
</html>

Now, the idea (just in case my js is so unintelligible that you can't figure out what I am trying to do) is that, when a user clicks the link (a#ccpt), the js will execute and the result (var result) will be displayed in the div (div#ccptresult). 现在,这个想法(以防万一我的js难以理解,以至于您无法弄清楚我要做什么)是,当用户单击链接(a#ccpt)时,该js将执行,结果( var结果)将显示在div(div#ccptresult)中。

I think it goes without saying that testing the page does absolutely nothing whatsoever so something is seriously not right but considering that, up until 7 days ago, I had never written a single line of js code, it's no surprise that my knowledge is nowhere near good enough to figure out where I have gone wrong on my own. 我想不用说测试页面绝对没有任何作用,所以有些事情是严重不正确的,但是考虑到直到7天前,我还没有写过一行js代码,所以我的知识还很遥远也就不足为奇了足以找出我自己哪里出错了。

Any help would be greatly appreciated. 任何帮助将不胜感激。

if i have understood correctly 如果我正确理解

$(function() {
        $('a#ccpt').click(function() {
          //execute the js function here
          $('div#ccptresult').append(result);
        });
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM