简体   繁体   English

如何在javascript中获取中间字符

[英]How to get the middle Characters in javascript

I just want to Get hello in Label1我只想在 Label1 中打个招呼

<script type="text/javascript">   
     result = "helloworld"         
     document.getElementById('<%= Label1.ClientID %>').innerHTML = result;
</script>

have you tried doing result.substring(0,5);你试过做 result.substring(0,5);

<script type="text/javascript">   
 result = "helloworld"         
 document.getElementById('<%= Label1.ClientID %>').innerHTML = result.substring(0,5);
</script>

With result.slice(start, end) you can cut the string.使用result.slice(start, end)您可以剪切字符串。 For "HelloWorld" that would look like对于"HelloWorld" ,看起来像
result.slice(0, 5) . result.slice(0, 5)

If you want to cut until the half of the string just do如果你想剪到弦的一半就做
result.slice(0, floor(result.length/2)) . result.slice(0, floor(result.length/2))

Source: https://www.w3schools.com/jsref/jsref_slice_string.asp来源:https://www.w3schools.com/jsref/jsref_slice_string.asp

I don't know about your label reference, but setting HTML with document.getElementById("mylabel").innerHTML = result;我不知道您的标签参考,但使用document.getElementById("mylabel").innerHTML = result;设置 HTML does the trick.诀窍。 Best try with a straight forward label like ("mylabel") for testing.最好尝试使用像("mylabel")这样的直接标签进行测试。

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

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