简体   繁体   English

如何使用window.open onClick-reactjs

[英]how to use window.open onClick - reactjs

I want to use window.open but I got this error message: 我想使用window.open但收到以下错误消息:

Uncaught Error: Expected onClick listener to be a function, instead got type string

my code: 我的代码:

<img id='drftrgvlnbpewmcswmcs' style={{cursor:'pointer'}} onClick='window.open("http://trustseal.enamad.ir/Verify.aspx?id=15288&p=nbpdyncrwkynaqgwaqgw", "Popup","toolbar=no, location=no, statusbar=no, menubar=no, scrollbars=1, resizable=0, width=580, height=600, top=30")' alt='' src='http://trustseal.enamad.ir/logo.aspx?id=15288&p=lznbfujyqesgukaqukaq'/>

You can't put plan string in onClick prop. 您不能将计划字符串放在onClick道具中。 You have to pass a function. 您必须传递一个函数。

Example

<img id='drftrgvlnbpewmcswmcs' style={{cursor:'pointer'}} onClick={() => window.open("http://trustseal.enamad.ir/Verify.aspx?id=15288&p=nbpdyncrwkynaqgwaqgw", "Popup","toolbar=no, location=no, statusbar=no, menubar=no, scrollbars=1, resizable=0, width=580, height=600, top=30")} alt='' src='http://trustseal.enamad.ir/logo.aspx?id=15288&p=lznbfujyqesgukaqukaq'/>

As it says Uncaught Error: Expected onClick listener to be a function, instead got type string 就像Uncaught Error: Expected onClick listener to be a function, instead got type string

It should be a function. 它应该是一个功能。 So you can do the followings 因此,您可以执行以下操作

openMyWindow = () => {
 window.open("http://trustseal.enamad.ir/Verify.aspx?id=15288&p=nbpdyncrwkynaqgwaqgw", "Popup","toolbar=no, location=no, statusbar=no, menubar=no, scrollbars=1, resizable=0, width=580, height=600, top=30")
}

and use it like 并像这样使用

<img id='drftrgvlnbpewmcswmcs' style={{cursor:'pointer'}} onClick={this.openMyWindow} alt='' src='http://trustseal.enamad.ir/logo.aspx?id=15288&p=lznbfujyqesgukaqukaq'/>

or of course this way as mentioned in other answers 或者当然是其他答案中提到的这种方式

onClick={() => {
    window.open("http://trustseal.enamad.ir/Verify.aspx?id=15288&p=nbpdyncrwkynaqgwaqgw", "Popup", "toolbar=no, location=no, statusbar=no, menubar=no, scrollbars=1, resizable=0, width=580, height=600, top=30")
  }}

You need to enclose your code with a function. 您需要使用功能将代码括起来。 For example: 例如:

<img
  id='drftrgvlnbpewmcswmcs'
  style={{ cursor: 'pointer' }}
  onClick={() => {
    window.open("http://trustseal.enamad.ir/Verify.aspx?id=15288&p=nbpdyncrwkynaqgwaqgw", "Popup", "toolbar=no, location=no, statusbar=no, menubar=no, scrollbars=1, resizable=0, width=580, height=600, top=30")
  }}
  alt=''
  src='http://trustseal.enamad.ir/logo.aspx?id=15288&p=lznbfujyqesgukaqukaq'
/>

i hope the code can be usefull for you 我希望代码对您有用

 <img
      id='drftrgvlnbpewmcswmcs' 
      style={{cursor:'pointer'}} 
      onClick={()=>{ window.open("http://trustseal.enamad.ir/Verify.aspx?id=15288&p=nbpdyncrwkynaqgwaqgw", "Popup","toolbar=no, location=no, statusbar=no, menubar=no, scrollbars=1, resizable=0, width=580, height=600, top=30") }} 
      alt=''
      src='http://trustseal.enamad.ir/logo.aspx?id=15288&p=lznbfujyqesgukaqukaq'
 />

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

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