【发布时间】:2019-06-03 16:35:53
【问题描述】:
获得了包含某些参数和动态值的“简码”。需要使用 javascript 提取这些值。 这就是短代码的样子 [offer-button width="500px" bg="#ff0000" color="#3366ff" padding="40px 50px" text="进来吧!" font="11px" weight="bold" style="italic"]
例如,我想提取宽度值(在本例中为“500px”)。 我决定在 'width="' 和 '"' 之间拉一段字符串。不过,我无法摆脱一种感觉——一定有更好的方法。 这是我尝试的代码
<div class="shortcode">[offer-button width="500px" bg="#ff0000" color="#3366ff" padding="40px 50px" text="Come on in!" font="11px" weight="bold" style="italic"]</div>
var wholeshort = $(".shortcode").html();
var widthstart = wholeshort.indexOf('width="') + 1;
var widthend = wholeshort.indexOf('"', widthstart);
var widthval = wholeshort.substring(widthstart,widthend);
有了这个,我得到 'idth=',而不是 '500px'
不知道我哪里出错了。
【问题讨论】:
标签: javascript jquery