简体   繁体   English

从标签上切一个星号

[英]Slicing an asterisk from a label

I am really new to JavaScript and jQuery. 我真的是JavaScript和jQuery的新手。 I have a form where I have all the required field labels appear with an asterisk (I can't change the HTML) and I plan to remove that and instead add a custom asterisk with its own class. 我有一个表单,其中所有必填字段标签都带有星号(我无法更改HTML),并且我打算删除该表单,而是使用其自己的类添加自定义星号。

I am unable to remove the askterisk whatsoever. 我无法删除任何问号。 It would be great if you could assist. 如果您可以提供帮助,那就太好了。 All my required field labels appear in the following class - "reg-required-field". 我所有的必填字段标签都显示在以下类别中-“ reg-required-field”。

Following is the Markup for one of the field and label. 以下是该字段和标签之一的标记。

<div class="reg-field-container">
          <div class="reg-process-row">
              <div class="reg-field-left-column">
                   <label class="reg-required-field" for="ctl00_ContentPlaceHolder1_tbxFirstName">*First name:</label>
              </div>
              <div class="reg-field-right-column">
                   <input name="ctl00$ContentPlaceHolder1$tbxFirstName" maxlength="30" id="ctl00_ContentPlaceHolder1_tbxFirstName" type="text">
              </div>
          </div>
      </div>

The JavaScript that I created - 我创建的JavaScript-

var myString = document.getElementsByClassName("reg-required-field").textContent;
var newString = myString.slice(1,myString.length);

Thanks! 谢谢!

You can use the String#charAt(index) method to test whether the first character of each .reg-required-field 's text content is an asterisk, and if it is, remove the first character from the text using text.slice(1) . 您可以使用String#charAt(index)方法测试每个.reg-required-field文本内容的第一个字符是否为星号,如果是,请使用text.slice(1) Note that the second argument of slice is, by default, the length of the string being sliced. 请注意,默认情况下, slice的第二个参数是要切片的字符串的长度。

document.querySelectorAll('.reg-required-field') can be substituted with document.getElementsByClassName('reg-required-field') if you need to support older browsers. document.querySelectorAll('.reg-required-field')可以与取代document.getElementsByClassName('reg-required-field')如果你需要支持旧的浏览器。

Edit: I added the ability to customize a .custom-asterisk in your HTML and CSS which will automatically replace the intial '*' in label text. 编辑:我添加了在HTML和CSS中自定义.custom-asterisk ,该功能将自动替换标签文本中的初始'*' I annotated the code below; 我注释了下面的代码; let me know if you have any questions. 如果您有任何问题,请告诉我。

 var customAsterisk = document.querySelector('.custom-asterisk') // detach the custom asterisk template from the DOM, so you don't see it customAsterisk.parentNode.removeChild(customAsterisk) ;[].forEach.call(document.querySelectorAll('.reg-required-field'), function (e) { var text = e.textContent if (text.charAt(0) === '*') { // remove the '*' e.textContent = text.slice(1) // insert a copy of the .custom-asterisk before the new text e.insertBefore(customAsterisk.cloneNode(true), e.firstChild) } }) 
 .custom-asterisk { color: #f00; } 
 <div class="reg-field-container"> <div class="reg-process-row"> <div class="reg-field-left-column"> <label class="reg-required-field" for="ctl00_ContentPlaceHolder1_tbxFirstName">*First name:</label> </div> <div class="reg-field-right-column"> <input name="ctl00$ContentPlaceHolder1$tbxFirstName" maxlength="30" id="ctl00_ContentPlaceHolder1_tbxFirstName" type="text"> </div> </div> </div> <!-- Customize me! --> <span class="custom-asterisk">*</span> 

className returns an array like object that you have to loop through in order to apply to every element. className返回一个类似对象的数组,您必须遍历该数组才能应用到每个元素。 This is because many elements have the same class. 这是因为许多元素具有相同的类。 The correct code would likely be (using a for loop to iterate over every element with that class, and your code to remove the first character) 正确的代码可能是(使用for循环遍历该类的每个元素,并使用代码删除第一个字符)

var labels = document.getElementsByClassName("reg-required-field");
for(var i = 0; i < labels.length; i++){
    var oldString = labels[i].textContent;
    var newString = oldString.splice(1, oldString.length);

    labels[i].textContents = newString; // Assign the new string to the element
}

you can shorten the code by combining several lines into one line 您可以通过将多行合并为一行来缩短代码

var labels = document.getElementsByClassName("reg-required-field");
for(var i = 0; i < labels.length; i++){       
   labels[i].textContent = labels[i].textContent.splice(1, labels[i].length);
}

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

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