我希望这个脚本触发一个javascript函数,而不是打招呼

I want this script to trigger a javascript function instead of saying hello

本文关键字:函数 javascript 打招呼 一个 脚本 我希望      更新时间:2023-09-26

我找到了一个有效的脚本,现在请记住,我根本不了解JQuery。我想要这个脚本:

document.getElementById("id_of_button").onclick = function() {clickFunction()};
function clickFunction() {alert("hello");}
$("#id_of_textbox").keyup(function(event){
    if(event.keyCode === 13){
        $("#id_of_button").click();
    }
});

触发此函数,而不是创建 Hello 弹出窗口

document.location.href = 'http://cse.google.com/cse?cx=009002930969338329916:kta6o_isob0&q=' + escape(document.getElementById('search-box').value)

下面编码你想要的

<input type="text" id="search-box">
<button id="id_of_button">Search</button>
<script type="text/javascript">
  document.getElementById("id_of_button").onclick = function() {clickFunction()};
  function clickFunction() {
    document.location.href =
        'https://cse.google.com/cse?cx=009002930969338329916:kta6o_isob0&q=' +
      escape(document.getElementById('search-box').value)
    }
  $("#id_of_textbox").keyup(function(event){
      if(event.keyCode === 13){
          $("#search-box").click();
      }
  });
</script>
$("#id_of_textbox").click(function () {
    document.location.href = 'http://cse.google.com/cse?cx=009002930969338329916:kta6o_isob0&q=' + escape($('#search-box').val())
});
$(document).keypress(checkKey);
function checkKey(e) {
    switch (e.keyCode) {
    case 13:
       document.location.href = 'http://cse.google.com/cse?cx=009002930969338329916:kta6o_isob0&q=' + escape($('#search-box').val())
        break;
    }
}

不完全清楚,但从有限的信息来看:

function clickFunction() {
  var mylink = escape(document.getElementById('search-box').value);
  document.location.href = 'http://cse.google.com/cse?cx=009002930969338329916:kta6o_isob0&q=' + mylink;
}

看起来整个事情可以写成:

$("#id_of_button").on('click', function() {
  var mylink = escape($('#search-box').value);
  document.location.href = 'http://cse.google.com/cse?cx=009002930969338329916:kta6o_isob0&q=' + mylink;
});
$("#id_of_textbox").keyup(function(event) {
  if (event.keyCode === 13) {
    $("#id_of_button").trigger('click');
  }
});