需要访问输入的所有标签值,然后在第二个下划线后截断
Need to access all the label values of an input and then truncate after the second underscore
我有一个页面,其中的标记看起来像这样。我正在尝试使用 jQuery 的每个。函数访问每个标签的值,然后在第二个下划线之后截断。
例如,此值 "M_1010_Lilac" 将更改为此值“丁香”
<div class="threads-swatch-wrapper"><input type="hidden" name="descriptive[51]" value="M_1010_Coral_Cloud" class="swatch-descriptive-name" />
<input type="radio" name="id[51]" value="2564" id="attrib-51-2564" class="threads-radio-btn" />
<label class="attribsRadioButton two thread-opts" for="attrib-51-2564">M_1010_Coral_Cloud<br />
<img src="images/attributes/Vineyard_Merino_Wool/M_1010_Coral_Cloud.jpg" alt="" width="260" height="320" /></label></div>
<div class="threads-swatch-wrapper"><input type="hidden" name="descriptive[51]" value="M_1010_Lilac" class="swatch-descriptive-name" />
<input type="radio" name="id[51]" value="2565" id="attrib-51-2565" class="threads-radio-btn" /><label class="attribsRadioButton two thread-opts" for="attrib-51-2565">M_1010_Lilac<br />
<img src="images/attributes/Vineyard_Merino_Wool/M_1010_Lilac.jpg" alt="" width="260" height="320" /></label></div>
我已经尝试过各种方法,但到目前为止没有任何效果。
任何帮助将不胜感激
我想我已经为您找到了答案。 Fiddle
可能不是最漂亮或最有效的答案,但它有效。
$('label').each(function() {
var str = $(this).text(), img;
var img_str = $(this).html();
var word, uh, x = [], z = 0;
for ( var i = 0; i < str.length; i++ ) {
if ( str.charAt(i) === '_' ) {
x[z] = i;
z++;
}
}
if ( img_str.search('<') != -1 ) {
img = img_str.substring(img_str.search('<'), img_str.length);
} else {
img = '';
}
if ( x.length > 2 ) {
uh = str.substring(x[1]+1, str.length);
word = uh.replace('_', ' ');
} else {
word = str.substring(x[1]+1, str.length);
}
$(this).html(word + img);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>M_1010_Coral_Cloud<br />
<img src="images/attributes/Vineyard_Merino_Wool/M_1010_Coral_Cloud.jpg" alt="" width="260" height="320" /></label> <br />
<label>M_1010_Lilac</label> <br />
<label>M_1010_Flower</label>
我有一个页面,其中的标记看起来像这样。我正在尝试使用 jQuery 的每个。函数访问每个标签的值,然后在第二个下划线之后截断。
例如,此值 "M_1010_Lilac" 将更改为此值“丁香”
<div class="threads-swatch-wrapper"><input type="hidden" name="descriptive[51]" value="M_1010_Coral_Cloud" class="swatch-descriptive-name" />
<input type="radio" name="id[51]" value="2564" id="attrib-51-2564" class="threads-radio-btn" />
<label class="attribsRadioButton two thread-opts" for="attrib-51-2564">M_1010_Coral_Cloud<br />
<img src="images/attributes/Vineyard_Merino_Wool/M_1010_Coral_Cloud.jpg" alt="" width="260" height="320" /></label></div>
<div class="threads-swatch-wrapper"><input type="hidden" name="descriptive[51]" value="M_1010_Lilac" class="swatch-descriptive-name" />
<input type="radio" name="id[51]" value="2565" id="attrib-51-2565" class="threads-radio-btn" /><label class="attribsRadioButton two thread-opts" for="attrib-51-2565">M_1010_Lilac<br />
<img src="images/attributes/Vineyard_Merino_Wool/M_1010_Lilac.jpg" alt="" width="260" height="320" /></label></div>
我已经尝试过各种方法,但到目前为止没有任何效果。 任何帮助将不胜感激
我想我已经为您找到了答案。 Fiddle
可能不是最漂亮或最有效的答案,但它有效。
$('label').each(function() {
var str = $(this).text(), img;
var img_str = $(this).html();
var word, uh, x = [], z = 0;
for ( var i = 0; i < str.length; i++ ) {
if ( str.charAt(i) === '_' ) {
x[z] = i;
z++;
}
}
if ( img_str.search('<') != -1 ) {
img = img_str.substring(img_str.search('<'), img_str.length);
} else {
img = '';
}
if ( x.length > 2 ) {
uh = str.substring(x[1]+1, str.length);
word = uh.replace('_', ' ');
} else {
word = str.substring(x[1]+1, str.length);
}
$(this).html(word + img);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>M_1010_Coral_Cloud<br />
<img src="images/attributes/Vineyard_Merino_Wool/M_1010_Coral_Cloud.jpg" alt="" width="260" height="320" /></label> <br />
<label>M_1010_Lilac</label> <br />
<label>M_1010_Flower</label>