在该代码中,如何捕获半冒号之后的所有数字?
忽略此rgFHTeVZ-4444322794,它仅出现是因为我正在使用预览链接。
<p id="productName">rgFHTeVZ-4444322794Diecut model number: 110</p>
productName是通过以下脚本中的URL参数动态添加的:
<script type='text/javascript'>
// start by creating a function
function loadUp(){
var str = decodeURIComponent(window.location.search.replace(/(?:(\D+=))/ig, "")) //get the search parameters from the url and remove everything before the "=" sign
document.getElementById('productName').innerHTML = str //assign that string to the "innerHTML" of the h1 tag that has an id of "productName"
};
window.onload = loadUp; // once the page has loaded, fire off that function
</script>
我想获取这些数字,并使用它们将其动态添加到img src属性中,以便图像将根据参数进行更改。
我想动态更改110。
img src如下所示:https : //cdn2.hubspot.net/hubf/43534/asset-diecut/sampleImage/110_a.png
任何帮助,将不胜感激,谢谢
1) get contents of div as string
2) split string into array broken on :
3) assign div new innerHTML
let text = document.getElementById('productName').innerHTML
let textArray = text.split(': ')
document.getElementById('productName').innerHTML = (textArray[1]).trim()
<p id="productName">rgFHTeVZ-4444322794Diecut model number: 110</p>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句