Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

javascript - Removing first string character with substr in WordPress

I am trying to remove the "|" from the file size span tag. The syntax of my javascript code so far does look fine, but it's not working quite yet.

From my understanding I am using the proper syntax for substr: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr

In Chrome, I am getting this console error:

Uncaught TypeError: Cannot read property 'substr' of undefined

In Firefox, I am getting this console error:

Uncaught TypeError: innerTextString is undefined.

Also the "|" isn't being removed as intended. Any ideas where I am going wrong here?

Thank you in advance.

<script>
    const prettyLinkRightFileSize = document.querySelectorAll('.prettyFileList .float_right:nth-child(1)');
    const innerTextString = prettyLinkRightFileSize.innerText;
    innerTextString.substr(0);
</script>
.prettyFileList .float_right {
    float: right;
}
<div class="prettyFileList">
    <div>
        <a href="#" class="prettylink">
            <span class="float_right">| Size 150 KB</span>
            <span class="float_right">28th Jan 2021</span> 
        </a>
    </div>
</div>
question from:https://stackoverflow.com/questions/65944647/removing-first-string-character-with-substr-in-wordpress

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You do not crop anything, when you use innerTextString.substr(0), as it has the same amount of characters like the original String.

var e = "ThisisaText";
var t = e.substring(0);

Log.v("e", e);
Log.v("t", t);

Output:

"ThisisaText"
"ThisisaText"

additionally you select the Items via classname. So I recommend, you crop all Items with the same class to provide consistency.

Try this code:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="prettyFileList">| Testsize</div>
</body>
<script>
    var c = document.getElementsByClassName('prettyFileList');
    for (var i = 0; i < c.length; ++i) {
      var item = c[i];  
      item.innerHTML = item.innerHTML.substr(1);
    }
</script>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...