douke8473 2016-08-26 19:01
浏览 35

JavaScript找出数字开始的位置并添加标记

I am working with 3D carts, they do not give me any access to PHP only JavaScript HTML and CSS. I want to get the dimensions of my products on the category page. but they need to be on a new line so that customers can clearly see it. If i add a <br /> into the title it messes up a lot of things.

If this was the name of the product I want to find the first number "16" and add a <br /> in front of it using JavaScript.

DELUXE TOTE JR. 16" X 12" X 6"

To get the result of

DELUXE TOTE JR. <br />
16" X 12" X 6"

Is this possible or a lost cause?

  • 写回答

2条回答 默认 最新

  • doushang8846 2016-08-26 19:25
    关注

    You could use Javascript's replace method to accomplish what you want, but like others mentioned, it's going to be tricky if different pieces of that string are wrapped in other elements like spans or divs, or if you don't have a clear selector around the whole string to start with. If you could post the actual HTML that is output, we could probably get this closer.

    var title = document.getElementById('title')
    var titleText = title.innerHTML;
    var res = titleText.replace(/([0-9]{1,2}")/, "<br/>$&");
    title.innerHTML = res;
    <span id='title'>DELUXE TOTE JR. 16" X 12" X 6"</span>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败