Javascript 如何在不同的URL上重定向图片点击?

Javascript 如何在不同的URL上重定向图片点击?,javascript,reactjs,Javascript,Reactjs,单击图像时,我试图重定向到指定的URL。 默认情况下,它会在URL的开头追加localhost,因此会将URL视为localhost的子域,并且无法访问 方法1: <a rel="noopener noreferrer" href={rowData.preview} target="_blank"> <Image className="preview-content" src={webAttachment} /> </a> 方

单击图像时,我试图重定向到指定的URL。 默认情况下,它会在URL的开头追加localhost,因此会将URL视为localhost的子域,并且无法访问 方法1:

<a rel="noopener noreferrer" href={rowData.preview} target="_blank">
   <Image
    className="preview-content"
    src={webAttachment}
   />
</a>

方法2:

<Link to={rowData.preview}>
    <Image
     className="preview-content"
     src={webAttachment}
    />
</Link>

{rowData.preview}包括任何网站的url。 在任何方法中都无法获得可靠的解决方案

onClick={()=> window.open("someLink", "_blank")}
访问stackoverflow链接

访问stackoverflow链接


试试这些代码行

import { Link } from "react-router-dom";


试试这些代码行

import { Link } from "react-router-dom";


  • href=“google.com”将导致“您当前的完整url/google.com”
  • href=“/google.com”将导致“您的域/google.com”
  • href=”https://google.com将导致“google.com”
不仅仅是反应。即使在普通的html中,它也以同样的方式工作。因此,请确保您正在传递的外部URL
rowData.preview
具有http://

    <a target="_blank" href="https://www.linkedin.com/">
       <img
           src={b4}
           alt="Linkedin"
           className="mw3-ns"
           style={{height: "50px", width: "50px", cursor: "pointer"}}
                                    
        />
     </a>
    
  • href=“google.com”将导致“您当前的完整url/google.com”
  • href=“/google.com”将导致“您的域/google.com”
  • href=”https://google.com将导致“google.com”
不仅仅是反应。即使在普通的html中,它也以同样的方式工作。因此,请确保您正在传递的外部URL
rowData.preview
具有http://


<a target="_blank" href="https://www.linkedin.com/">
   <img
       src={b4}
       alt="Linkedin"
       className="mw3-ns"
       style={{height: "50px", width: "50px", cursor: "pointer"}}
                                
    />
 </a>
这对我有用。虽然我因为使用target=“\u blank”



这对我有用。虽然我收到了使用target=“\u blank”

的警告,但如果指定的URL是react路由,则应使用第二种方法。如果是外部URL,则URL必须是绝对URL。我的意思是,它应该从开始。它是外部URL,例如:它正在考虑作为本地主机:3000/google.com尝试将链接直接放入
src
,而不包装到
rowData。预览
并检查是否得到所需的结果?如果指定的URL是反应路径,则应使用第二种方法。如果是外部URL,则URL必须是绝对URL。我的意思是,它应该从开始。它是外部URL,例如:它正在考虑作为本地主机:3000/google.com尝试将链接直接放入
src
,而不包装到
rowData。预览
并检查是否得到所需的结果?如果我的URL是,那么它会正确地重定向我。如果它只是google.com,它会将我重定向到这个位置。如果我的url是,那么它会正确地重定向我。如果只是google.com,它会将我重定向到此位置。它会在开头追加localhost:3000/。它会在开头追加localhost:3000/。当用户在href中输入“google.com”或“/google.com”或“https://google.com”时如何处理?有什么建议吗?您可以使用userdata.replace(/([\s]+)/g,”);这样在输入的URL中就不会有任何空格。if(userdata.indexOf(“/”==0){userdata=userdata.slice(1,userdata.length-1)};当用户在href中输入“google.com”或“/google.com”或“https://google.com”时,如何处理?有什么建议吗?您可以使用userdata.replace(/([\s]+)/g,”);这样在输入的URL中就不会有任何空格。if(userdata.indexOf(“/”==0){userdata=userdata.slice(1,userdata.length-1)};