问:
如果我有一个我知道的具有特定类名的div,并且有一个标题(属性),我怎么能用Jquery获得这个标题值的特定部分?
源代码:(来自火虫)
<div style="height: 21px; width: 90%; left: 0%;" class="rsApt" title="WPF[003]" id="rs_7_0">
<div style="background-color: rgb(102, 204, 102);" class="rsAptOut">
<div class="rsAptMid">
<div class="rsAptIn">
<div class="rsAptContent">
WPF[003]<a href="#" class="rsAptDelete" style="visibility: hidden;">delete</a>
</div>
</div><div style="z-index: 80;" class="rsAptResize">
<!-- -->
</div>
</div>
</div>
</div>
如果我想获得title 003
的title属性(可能是其他Div中具有相同类别的其他数字),该怎么做?如果有任何一般方法来获得字符串的一部分,这将是伟大的..
我想根据以下代码设置下拉列表的选定值:
$(document).ready(function() {
$(".rsAptContent").click(function(e) {
if ($(e.target).hasClass('rsAptDelete')) {
}
else {
$("#ddl_editCourse").val('The accessed value of the Div title');
ShowDialog(true);
e.preventDefault();
}
});
如何用这行代码做到这一点??
EDITED
感谢。
答案 0 :(得分:2)
var regex = new RegExp("\\[(.*)\\]");
$("#ddl_editCourse").val(regex.exec($(".rsApt").attr("title"))[1]);
答案 1 :(得分:2)
您可以在此处使用正则表达式,也可以使用substring
和indexOf
方法。
如果您知道数字将始终在方括号内,那么您可以使用indexOf查找它们,根据这些索引获取子字符串:
var title = $('#rsApt').attr('title');
var opening = title.indexOf('[');
var closing = title.indexOf(']');
var number = title.substring(opening, closing);
//打印出数字
$("#ddl_editCourse").val('The accessed value of the Div title: ' + number);
答案 2 :(得分:1)
$("#ddl_editCourse").val(new RegExp(/\d{3}/).exec( $(".rsApt").attr("title") ));
答案 3 :(得分:1)
$("#ddl_editCourse").val( $(".rsApt").attr("title").replace(/\D/g,'') );