简体   繁体   English

单击按钮时在Jquery函数中转换脚本

[英]Convert script in Jquery function when clicked on button

I am trying to integrate a button function in jquery, I am a learner of jquery but i am not sure how scripting can be converted into jquery 我正在尝试将按钮功能集成到jquery中,我是jquery的学习者,但我不确定如何将脚本转换为jquery

Here is a code 这是一个代码

<script type="text/javascript">
var _hmc = _hmc || [];
_hmc.push(['app', '<application ID>']);
_hmc.push(['title','Talk with us.']);
(function() {
var hm = document.createElement('script'); hm.type = 'text/javascript';
hm.async = true;
hm.src = 'https://hipmob.s3.amazonaws.com/hipmobchat.min.js';
var b = document.getElementsByTagName('script')[0]; b.parentNode.insertBefore(hm, b);
})();
</script>

I need to place it inside button click 我需要将其放置在按钮内

 jQuery('.live-chat').click(function()
   {

   });

Also if anyone would like to elaborate how things work eg what is || 另外,如果有人想详细说明事物是如何工作的,例如||是什么。 [] etc []等

The code you would need to do what you want is the following: 您所需要做的代码如下:

jQuery('.live-chat').click(function() {
    var _hmc = _hmc || [];
    _hmc.push(['app', '<application ID>']);
    _hmc.push(['title','Talk with us.']);

    var hm = document.createElement('script');
    hm.type = 'text/javascript';
    hm.async = true;
    hm.src = 'https://hipmob.s3.amazonaws.com/hipmobchat.min.js';
    var b = document.getElementsByTagName('script')[0];
    b.parentNode.insertBefore(hm, b);

    return false;
});

As for your questions: 至于您的问题:

  1. || means OR in javascript 在JavaScript中表示OR
  2. [] is shorthand for creating a new array, or an 'array literal' []是创建新数组或“数组文字”的简写
  3. This means the line var _hmc = _hmc || []; 这意味着线var _hmc = _hmc || []; var _hmc = _hmc || []; means _hmc equals _hmc OR a new array 表示_hmc等于_hmc或一个新数组

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

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