简体   繁体   English

苹果.replace()HTML元素由把手js生成

[英]apple .replace() Html element generate by handlebar js

I am wondering if how am i able to change the element data by .replace() if i use handlebar js to generate html elements. 我想知道如果我使用车把js生成html元素,如何通过.replace()更改元素数据。

For instance i have this role of p tag which display a row of data by handlebar js: 例如我有p标签的作用,它通过车把js显示一行数据:

<p id="pre-region">{{region}}</p>

and the result of it is 结果是

1,44 1,44

and i 'd like to change it to 我想将其更改为

1+44 1 + 44

If you haven't had any experience of handlebar js then consider the tag be 如果您没有任何关于把手js的经验,请考虑将标签设为

 <p id="pre-region">1,44</p>

how should i change from 1,44 to 1 +44? 我应该如何从1,44更改为1 +44?

UPDATE 1 更新1

Here should be an extersion for my question. 这应该是我的问题了。 I am passing the HTML element inside pre-region into an href in order to update my website by Ajax. 我正在将预区域内的HTML元素传递到href中,以便通过Ajax更新我的网站。

After i have converted all the comma in to "+" the API retrieve special character "&B2" which equal to the symbol "+" and the API goes error. 在我将所有逗号都转换为“ +”之后,API检索了特殊字符“&B2”,该字符等于符号“ +”,并且API出错。

MYDOMAIN/path/getRegion?token&profileId=111&dataType=all&region=1%2B4 MYDOMAIN /路径/ getRegion?令牌简档= 111&的dataType =所有&区域= 1%2B4

This is how may API looks like at the moment 这就是目前的API外观

MYDOMAIN/path/getRegion?token&profileId=111&dataType=all&region=1+4 MYDOMAIN /路径/ getRegion?令牌简档= 111&的dataType =所有&区域= 1 + 4

should be the solution 应该是解决方案

I haven't had any experience of handlebars.js but from my point of view, you can just put the code just before the </body> : 我没有任何handlebars.js经验,但是从我的角度来看,您可以将代码放在</body>

<script>
  var node = document.getElementById('pre-region');
  node.innerHTML = node.innerHTML.replace(',', '+');
</script>

I'll check out the handlebars js in case it does not work. 如果不起作用,我将检查车把js。

Update: As you mentioned in the comment, if you need to use it in the HTTP request/URL, you may handle the string using decodeURIComponent(yourstring) : 更新:如评论中所述,如果需要在HTTP请求/ URL中使用它,则可以使用decodeURIComponent(yourstring)处理字符串:

decodeURIComponent('1%2B44'); // you get '1+44'

Read more about decodeURIComponent() method from this . this阅读更多有关decodeURIComponent()方法的信息。 In URL, it should be encoded as region=1%2B44 in your case; 在URL中,您应将其编码为region=1%2B44 while it should be decoded if you want to use it in your JavaScript code or display in the web page. 如果您想在JavaScript代码中使用它或将其显示在网页中,则应将其解码。

Update 1 更新1

You should encode your string when it's used as a part of parameter of HTTP request. 当字符串用作HTTP请求参数的一部分时,应该对字符串进行编码。 Therefore, it looks good if the URL is: 因此,如果URL为:

MYDOMAIN/path/getRegion?token&profileId=111&dataType=all&region=1%2B4 MYDOMAIN /路径/ getRegion?令牌简档= 111&的dataType =所有&区域= 1%2B4

What you need to do is decode the string on your server side . 您需要做的是在服务器端解码字符串。 I assume that you are in control of the server side. 我假设您在服务器端。 If you are using Node.js, you can just use decodeURIComponent() method to decode the parameter. 如果使用的是Node.js,则可以只使用decodeURIComponent()方法对参数进行解码。 If you're using Python or PHP as your server language, it should be something like decodeURIComponent() in that language. 如果您使用Python或PHP作为服务器语言,则应使用该语言编写类似decodeURIComponent()

Update 2 更新2

The solution above only replace the first occasion of comma to + . 上面的解决方案仅将逗号替换为+的第一次。 To solve that, simply use: 要解决这个问题,只需使用:

<script>
  var node = document.getElementById('pre-region');
  node.innerHTML = node.innerHTML.replace(/,/g, '+'); 
  // Regular Expression is used here, 'g' for global search. 
</script>

PHP has a replaceAll() method, so we can add that method to String.prototype like below if you want: PHP有一个replaceAll()方法,因此我们可以根据需要将该方法添加到String.prototype如下所示:

<script>
  String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.split(search).join(replacement);
  }
  // Another method to replace all occasions using `split` and `join`. 
</script>

Alright, so this is my first answer ever on stack overflow so I'm alien to this whole thing but here we go: 好了,这是我在堆栈溢出时的第一个答案,因此我对整个事情都陌生,但是我们开始:

You could try this code in another js file that runs after handlebars: 您可以在另一个在把手之后运行的js文件中尝试以下代码:

var pre = $('#pre-region'); // defines a variabe for the element (same as
                            // document.getElementById('pre-region'))
var retrievedRegion = pre.innerHTML;
var splitten = retrievedRegion.split(',');
var concatenated = parseInt(split[0]) + parseInt(split[1])
retrievedRegion.innerHTML = "'" + concatenated) + "'";

or using replace(): 或使用replace():

retrievedRegion.replace(',','+')

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

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