简体   繁体   中英

how to get a part of attribute value?

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

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

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);
$("#ddl_editCourse").val(new RegExp(/\d{3}/).exec( $(".rsApt").attr("title") ));
$("#ddl_editCourse").val( $(".rsApt").attr("title").replace(/\D/g,'') );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM