如何使用window.open onClick-reactjs

时间:2018-06-27 07:41:08

标签: javascript reactjs

我想使用window.open,但收到以下错误消息:

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

我的代码:

<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'/>

4 个答案:

答案 0 :(得分:1)

您不能将计划字符串放在onClick道具中。您必须传递一个函数。

示例

<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'/>

答案 1 :(得分:0)

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

它应该是一个函数。因此,您可以执行以下操作

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")
}

并像

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

或者当然是其他答案中提到的方式

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")
  }}

答案 2 :(得分:0)

您需要使用功能将代码括起来。例如:

<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'
/>

答案 3 :(得分:0)

我希望该代码对您有用

 <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'
 />