javascript - react 按钮不会弹出链接页面

标签 javascript reactjs hyperlink react-router href

单击按钮时,会在新选项卡中打开一个空白页面。我希望能够单击并获取新选项卡以打开显示的不同站点。

我发现像下面这样的链接是有效的:

btnClick() {
        window.open("https://www.google.com");
    } 
....................
<ButtonComponent cssClass='e-link' onClick={this.btnClick.bind(this)}>Go to google</ButtonComponent>
<小时/>
import { enableRipple } from '@syncfusion/ej2-base';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
class App extends React.Component {
    // Click Event.
    btnClick() {
        window.open();
    }
    render() {
        return (<div>
                <ButtonComponent cssClass='e-link' onClick={this.btnClick.bind("https://www.google.com")}>Go to google</ButtonComponent>
            </div>);
(<div>
                <ButtonComponent cssClass='e-link' onClick={this.btnClick.bind("https://www.yahoo.com")}>Go to yahoo</ButtonComponent>
            </div>);
    }
}
ReactDom.render(<App />, document.getElementById('button'));
<小时/>

没有错误,只是没有链接到预期的网站。

最佳答案

我相信您错误地使用了bind。您不在bind 中传递函数参数,要在onClick() 中传递带参数的函数,您可以使用箭头函数。尝试以下代码:

import { enableRipple } from "@syncfusion/ej2-base";
import { ButtonComponent } from "@syncfusion/ej2-react-buttons";
import * as React from "react";
import * as ReactDom from "react-dom";
enableRipple(true);
class App extends React.Component {
  constructor(props) {
    super(props)
    this.btnClick = this.btnClick.bind(this)
  } 

  btnClick(url) {
    window.open(url);
  }
  render() {
    return (<div>
      <ButtonComponent cssClass='e-link' onClick={() => this.btnClick("https://www.google.com")}>Go to google</ButtonComponent>
    </div>);
  }
}
ReactDom.render(<App />, document.getElementById("button"));

关于javascript - react 按钮不会弹出链接页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57236907/

相关文章:

javascript - 从 JavaScript 在表中打印文本的奇怪行为

reactjs - SwiperJS - ReactJS 旋转木马与视频和图像混合双响

javascript - 将 HTML 链接添加到 Google map 圆形元素

javascript - 如何使用附加属性扩展映射的 knockout View 模型 "on the fly"?

javascript - 错误: mongoose.模型不是函数

javascript - 如何在 CI 中使用 javascript

javascript - 应用程序初始化代码在 React 应用程序中放在哪里?

CSS 模块组合不工作

php - 没有结束分隔符 '/' 发现错误

java - 使用 Java 代码通过 jsoup 选择表中的链接