简体   繁体   English

如何获得属性值的一部分?

[英]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?? 如果我有一个具有特定类名的div并知道一个标题(属性),那么如何使用Jquery获得此标题值的特定部分?

the source code:(from fire bug) 源代码:(来自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? 如果我想获得title属性的003部分(可能是其他Divs中具有相同类的其他数字),该怎么做? 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. 您可以在此处使用正则表达式,也可以使用substringindexOf方法。

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: 如果您知道数字将始终在方括号内,则可以使用indexOf查找它们,并基于这些索引获取子字符串:

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,'') );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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