简体   繁体   English

JS函数没有在反应中的字符串html内被调用

[英]JS function not getting called inside string html in react

I am using string html and there is an image in it.我正在使用字符串 html,其中有一个图像。 I have to call onclick function on it.我必须在其上调用 onclick 函数。 Onclick function works but for alert only. Onclick 功能有效,但仅用于警报。 when I call a JS function it doesn't get called.当我调用 JS 函数时,它不会被调用。 I have used this.bind, arrow function and every possible way but no result.我用过 this.bind、箭头函数和所有可能的方法,但没有结果。 It is in react, pasting only relevant code here:它正在反应中,仅在此处粘贴相关代码:

 import pic1 from '../../assets/img/1.png' class MapModule extends Component { _isMounted = false; constructor(props) { super(props); this.state = { id: null, redirect: false, store:props.latlng, toggleFlag: false }; this.Toggle = this.Toggle.bind(this); } Toggle () { alert('Clicked'); } let content1 = ` <div class="opacityEffect"> <img width=100% onclick=${this.Toggle} src=${pic1}> </div>`; // This in event on infowindow for google maps marker.addListener('click',function () { map.setZoom(22); map.setTilt(45); infowindow.setContent(content1) infowindow.open(map, marker); });

PS: Everything runs fine, its 200 lines of code. PS:一切运行良好,它的 200 行代码。 Onclick of map marker infowindow gets open.地图标记信息窗口的点击打开。 Now I want to open a new window onClick of infowindow.现在我想打开一个新窗口 onClick of infowindow。 Thanks in advance!提前致谢!

As I see in your code, you used a normal HTML onclick event to call the React function.正如我在您的代码中看到的,您使用了一个普通的 HTML onclick 事件来调用 React 函数。 In React JS, the syntax of onclick event is as shown below.在 React JS 中,onclick 事件的语法如下所示。

<button onClick={activateLasers}>
   Activate Lasers
</button>

As I observe, you need to use the "C" in onclick.正如我所观察到的,您需要在 onclick 中使用“C”。 I have had the same issue when I am first working on Riot.js and it fixed when I changed to lowercase letters.我第一次在 Riot.js 上工作时遇到了同样的问题,当我改成小写字母时它就解决了。

And I don't think you need to use "this" inside React events.而且我认为您不需要在 React 事件中使用“this”。

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

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