我有一个带有li
元素的导航栏。我将类active
添加到与变量$page
对应的元素中。
对于每个列表元素,我放置与此类似的代码:
<li<?= $page=="new" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=new">New event</a></li>
这样,使用此示例,如果$page
为"new"
,我会将active
类添加到li
元素。
这适用于顶级列表项以及下拉列表。
我希望下拉列表子元素的父元素在其中一个子元素处于活动状态时也显示active
类。
要使用包含页面值"new"
,"existing"
和"gateway"
的下拉列表执行此操作,我会使用:
<li class="dropdown<?= $page=="new"||$page=="existing"||$page="gateway" ? " active" : null ?>">
然而,这不起作用;在Chrome开发人员工具中检查元素时,唯一的类是dropdown1
,并且任何其他添加的类都已消失。
在Chrome开发者工具中手动添加active
类会产生正确的结果。
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li<?= $page=="dashboard" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=dashboard">Dashboard</a></li>
<li class="dropdown<?= $page=="new"||$page=="existing"||$page="gateway" ? " active" : null ?>">
<a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Events <span class="caret"></span></a>
<ul class="dropdown-menu">
<li<?= $page=="new" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=new">New event</a></li>
<li<?= $page=="existing" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=existing">Existing events</a></li>
<li<?= $page=="gateway" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=gateway">Gateway</a></li>
</ul>
</li>
<li><a href="<?= $rootUrl ?>?logout=true">Sign out</a></li>
</ul>
</div>
当其子元素之一具有active
类时,如何使下拉列表的父元素具有active
类?
答案 0 :(得分:1)
"="
$page="gateway"
$page=="gateway"
<li class="dropdown<?= $page=="new"||$page=="existing"||$page="gateway" ? " active" : null ?>">
错过了double u;
srand(unsigned(time(NULL)));
for(int i = 0; i < 10; i++){
u = (double)rand()/(RAND_MAX+1) + (rand()%101); //Acquire random number, turn it into decimal and then add it to another number between 0-100.
u *= 10; //Shift numbers left to remove first decimal from round.
round(u); //Round to nearest whole number.
u /= 10; //Shift right to return the first decimal.
cout<<u<<" "<<flush;
}
public class CustomerAdapter extends ArrayAdapter<ShareWithArticle> {
private final Context mContext;
private final List<ShareWithArticle> mDepartments;
private final List<ShareWithArticle> mDepartments_All;
private final List<ShareWithArticle> mDepartments_Suggestion;
private final int mLayoutResourceId;
SharedFragment fragment;
public CustomerAdapter(Context context, int resource, List<ShareWithArticle> departments,SharedFragment fragment) {
super(context, resource, departments);
this.mContext = context;
this.mLayoutResourceId = resource;
this.mDepartments = new ArrayList<>(departments);
this.mDepartments_All = new ArrayList<>(departments);
this.mDepartments_Suggestion = new ArrayList<>();
this.fragment = fragment;
}
public int getCount() {
return mDepartments.size();
}
public ShareWithArticle getItem(int position) {
return mDepartments.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(mLayoutResourceId, parent, false);
}
ShareWithArticle department = getItem(position);
final TextView name = (TextView) convertView.findViewById(R.id.textView2);
final TextView userId = (TextView) convertView.findViewById(R.id.textView3);
name.setText(department.getUsrCompNm());
userId.setText(department.getUsrID() + "");
convertView.setOnClickListener(new Listener(name,userId));
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
class Listener implements View.OnClickListener
{
TextView name;
TextView userId;
Listener(TextView name, TextView userId)
{
this.name = name;
this.userId = userId;
}
@Override
public void onClick(View v) {
String userNm = name.getText().toString();
String userIdtxt = userId.getText().toString();
//create a method in fragment class and pass the data using tthe object.
fragment.setData(userNm,userIdtxt);
}
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
public String convertResultToString(Object resultValue) {
return ((ShareWithArticle) resultValue).getUsrCompNm();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
mDepartments_Suggestion.clear();
for (ShareWithArticle department : mDepartments_All) {
if (department.getUsrCompNm().toLowerCase().startsWith(constraint.toString().toLowerCase())) {
mDepartments_Suggestion.add(department);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = mDepartments_Suggestion;
filterResults.count = mDepartments_Suggestion.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mDepartments.clear();
if (results != null && results.count > 0) {
// avoids unchecked cast warning when using mDepartments.addAll((ArrayList<Department>) results.values);
List<?> result = (List<?>) results.values;
for (Object object : result) {
if (object instanceof ShareWithArticle) {
mDepartments.add((ShareWithArticle) object);
}
}
} else if (constraint == null) {
// no filter, add entire original list back in
mDepartments.addAll(mDepartments_All);
}
notifyDataSetChanged();
}
};
}
}