JavaScript Number-String DIV onclick奇怪的转换 [英] Javascript Number-String DIV onclick strange conversion

查看:35
本文介绍了JavaScript Number-String DIV onclick奇怪的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上要做的是将人的Facebook ID分配给DIV中的onclick-Event. div本身就是字符串,因为我稍后会通过innerHTML将其分配给DOM属性

What I basically do is that I assign the Facebook-ID from a person to a onclick-Event in a DIV. The div is a string itself because I assign it later to a DOM-property via innerHTML

"<div onclick='openFbFriend(" + friendsArray[i]['facebookID'] + ");' class='row'>";

现在,openFbFriend确实只是打开了此人的Facebook页面:

Now the openFbFriend does simply open the Facebook-Page of this person:

function openFbFriend(fbID) {
    window.open("https://www.facebook.com/" + fbID, "_blank");
}

对大多数人有用.我现在遇到的是某些链接断开了,我将重定向例如转到此页面: https://www.facebook.com/10204840506352732

Does work for most people. What I experienced now is that some Links are broken I'll redirected e.g. to this page: https://www.facebook.com/10204840506352732

我进行了调查,但很奇怪,但是发生的情况是,当FB-ID的长度大于或等于17个数字时,我的字符串从div更改为带有加号或未成年人的openFbFriend1.这怎么可能? fb-id永远不会转换为数字,并且始终是字符串.因此,如果我在div之后console.log fbid,则ID是正确的.但是……然后单击时会发生奇怪的情况,因为如果fbid> = 17digits(在openFbFriend中)是+ -1. 任何帮助表示赞赏:)!

I investigated and very strange but it happens that when the FB-ID is longer than or equal to 17 digits than my string is changed from the div to openFbFriend with plus or minors 1. How could that happen? The fb-id is never converted to a number and is always a string. So if I console.log the fbid after div the id is correct. But sth. weird happens then when clicked because if fbid>=17digits than in openFbFriend it is +-1. Any help appreciated :)!

推荐答案

也许您应该尝试

"<div onclick='openFbFriend(\"" + friendsArray[i]['facebookID'] + "\");' class='row'>";

因为如果参数周围没有\",则它将被解释为整数,或者如果包含非数字字符,则将导致错误.

because if you do not have the \" around the argument, then it will be interpreted as an integer or if it contains non-numeric characters then it will result in an error.

结果字符串将是:

<div onclick='openFbFriend("10204840506352732");' class='row'>

但是在您的版本中:

<div onclick='openFbFriend(10204840506352732);' class='row'>

这篇关于JavaScript Number-String DIV onclick奇怪的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆