从链接运行脚本

我有以下脚本在 div 中显示我的信息,它工作得很好,但有一个按钮......我试图让它从一个链接走,我尝试了很多方法,但没有,有什么想法吗?


<script>

function showCustomer(str) {


  var xhttp;    

  if (str == "") {

    document.getElementById("contenido").innerHTML = "";

    return;

  }

  xhttp = new XMLHttpRequest();

  xhttp.onreadystatechange = function() {

    if (this.readyState == 4 && this.status == 200) {

      document.getElementById("contenido").innerHTML = this.responseText;

    }

  };

  xhttp.open("GET", "getcustomer.php?q="+str, true);

  history.pushState(null, "", str+ ".xhttp");

  xhttp.send();

}

</script>


<input type='button' value='6df67913c1' onclick='showCustomer(this.value);' />


肥皂起泡泡
浏览 110回答 3
3回答

POPMUISE

你必须做这样的事情const myLinkButton = document.getElementById('my-Link-button')&nbsp; ;myLinkButton.onclick=e=>&nbsp; {&nbsp;&nbsp; e.preventDefault()&nbsp; // -> disable link action for new url&nbsp; console.log( myLinkButton.dataset.value )&nbsp; }<a href="#" data-value="6df67913c1" id="my-Link-button" >button by link</a>

qq_笑_17

利用<a href="JavaScript: void (0)" onclick="callYourFunction()>link<\a>

慕妹3146593

你可以这样做。&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; function showCustomer(element) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const str = element.getAttribute('data-value')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var xhttp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (str == "") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("contenido").innerHTML = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhttp = new XMLHttpRequest();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhttp.onreadystatechange = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.readyState == 4 && this.status == 200) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("contenido").innerHTML = this.responseText;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhttp.open("GET", "getcustomer.php?q="+str, true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; history.pushState(null, "", str+ ".xhttp");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhttp.send();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script>&nbsp; &nbsp; <a href="#" onclick="showCustomer(this)" data-value="6df67913c1">Same, from link</a>
打开App,查看更多内容
随时随地看视频慕课网APP