gpt4 book ai didi

JavaScript 不是 "opening"url

转载 作者:行者123 更新时间:2023-11-28 13:17:10 24 4
gpt4 key购买 nike

我不确定这个脚本有什么问题。它“应该”有效。

HTML

<img src="https://placehold.it/350x150" alt="" onclick="goTo('img1');">

Javascript

function goTo(img) {
if( img === 'img1' ) { window.open('https://www.google.com/','_blank'); }
else if( img === 'img2' ) { window.open('https://www.yahoo.com/','_blank'); }
etc...
else{ alert("This image is not recognized: " + img ); }
}

JsFiddle: https://jsfiddle.net/omarjuvera/xuzc70gn/2/

<小时/>

编辑:在网站上,我在 .ready() 中有 javascript 代码,如下所示:

$( document ).ready(function() {

function goTo(img) {
if( img === 'ivory' ) { window.open('https://www.indiegogo.com/project/preview/a0d4cff9','_blank'); }
else{ alert("This image is not recognized: " + img ); }
}

...more functions...
});

最佳答案

只需将 goTo 函数放在 .ready() 事件之外,如下所示:

function goTo(img) {
if( img === 'ivory' ) { window.open('https://www.indiegogo.com/project/preview/a0d4cff9','_blank'); }
else{ alert("This image is not recognized: " + img ); }
}

$( document ).ready(function() {

...more functions...


});

关于JavaScript 不是 "opening"url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36821313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com