获取具有与变量相等的属性的元素的属性

时间:2016-03-06 01:07:58

标签: jquery

让我们假设我有这个:

<a href="/index" data-title="Home">LINK</a>
<a href="/page2" data-title="Another Page">LINK</a>

我必须从data-title标记中获取a href,其location.pathname等于 public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { private Calendar dateTime = Calendar.getInstance(); @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Using current date as start Date final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); // Get DatePicker Dialog return new DatePickerDialog(getActivity(), this, year, month, day); } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { TextView chooseDateLabel = (TextView)getActivity().findViewById(R.id.chooseDateLabel); dateTime.set(year, monthOfYear, dayOfMonth); final int myYear = year; final int myMonth = monthOfYear; final int myDay = dayOfMonth; chooseDateLabel.setText(new StringBuilder().append(myMonth + 1).append("/").append(myDay) .append("/").append(myYear).append(" ")); DatePicker datePicker = (DatePicker)getActivity().findViewById(R.id.saveJobDatePicker); datePicker.init(myYear, myMonth, myDay, new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { year = myYear; monthOfYear = myMonth; dayOfMonth = myDay; ContentValues values = new ContentValues(); values.put("productDate", year + monthOfYear + dayOfMonth); } }); ContentValues values = new ContentValues(); values.put("date", myDay + myMonth + myYear); } }

如何使用jQuery执行此操作?

1 个答案:

答案 0 :(得分:3)

使用以下代码:

$('a[href="' + location.href + '"]').attr('data-title')

请参阅下面的工作代码示例,但location.href已替换为/index以显示该代码段正常运行:

alert($('a[href="' + '/index' + '"]').attr('data-title'))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="/index" data-title="Home">LINK</a>
<a href="/page2" data-title="Another Page">LINK</a>