开发者

how to get a part of attribute value?

开发者 https://www.devze.com 2023-02-19 16:30 出处:网络
Q: If i havea div with a specific class name i know , and have a title(attribute),How can i get a specific part of this titl开发者_Go百科e value with Jquery??

Q:

If i have a div with a specific class name i know , and have a title(attribute),How can i get a specific part of this titl开发者_Go百科e value with Jquery??

the source code:(from fire bug)

<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>

if i wanna to get this part 003 of the title attribute(may be other number in the other Divs with the same class), how to do that? and if there is any general way to get a part of string, this will be great..

i wanna to set the selected value of a drop down list with this value according to this code:

$(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();

            }

        });

how to do this with this line of code??

EDITED

Thanks.


You can use a regular expression here, or you can use the substring and indexOf methods.

If you know the number will always be within square brackets, then you can look for them using indexOf, the get the substring based on those indices:

var title = $('#rsApt').attr('title');
var opening = title.indexOf('[');
var closing = title.indexOf(']');
var number  = title.substring(opening, closing);

// print out the number

$("#ddl_editCourse").val('The accessed value of the Div title: ' + number);


var regex = new RegExp("\\[(.*)\\]");
$("#ddl_editCourse").val(regex.exec($(".rsApt").attr("title"))[1]);


$("#ddl_editCourse").val(new RegExp(/\d{3}/).exec( $(".rsApt").attr("title") ));


$("#ddl_editCourse").val( $(".rsApt").attr("title").replace(/\D/g,'') );
0

精彩评论

暂无评论...
验证码 换一张
取 消