jQuery简单的标签

时间:2011-02-12 18:11:48

标签: jquery css

我用jQuery创建了简单的选项卡 - 隐藏并显示相应的div onClick。 请参见此处示例http://jsfiddle.net/DCNpK/1/ 一切正常 - 我的问题是关于以下功能:

var activeTab = $(this).find("a").attr("href");
$(activeTab).show();

任何人都可以解释为什么 - $(activeTab).show(); - 显示正确的div,即使 - var activeTab - 指的是href的值,而不是div的id?

2 个答案:

答案 0 :(得分:4)

因为在href中有一个字符串“#div1”,它是jQuery选择器所以$("#div1")为你提供了正确的div

答案 1 :(得分:2)

检查下面给出的链接,这通过比较id和data_target属性显示了一个简单的标签:

public class FeaturedlistAdapter extends RecyclerView.Adapter<FeaturedlistAdapter.FeaturedView>
{
    private Context context;
    private List<Featured_data> featured_data;

    private final SparseBooleanArray mSparseBooleanArray;


    public FeaturedlistAdapter(Context context, List<Featured_data> featured_data,MyAdapterListener  myAdapterListener) {
        this.context=context;
        this.featured_data=featured_data;
        this.onclickListener=myAdapterListener;
        // init the flag
        mSparseBooleanArray = new SparseBooleanArray();


    }

    @NonNull
    @Override
    public FeaturedView onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_allbrands, viewGroup, false);
        return new FeaturedView(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FeaturedView featuredView, int i) {

        String price = featured_data.get(i).getProductPrice();
        String name = featured_data.get(i).getProductName();
        String quantity = featured_data.get(i).getProductQty();
        featuredView.getTxt_brandname().setText(featured_data.get(i).getProductName());
        String wishlist=featured_data.get(i).getWishlist();
        Picasso.get().load(HomeConstants.BASE_URL + featured_data.get(i).getProductImage()).placeholder(R.drawable.whitebackground).into(featuredView.img_brand);
        if (price != null && !price.equals("")) {
            String total_price = price + " Rs";
            featuredView.getTxt_brand_price().setText(total_price);
        }
        if (quantity != null && !quantity.equals("")) {
            String total_quantity = quantity + " Items";
            featuredView.getTxt_brand_item().setText(total_quantity);
        }


        if(wishlist!=null)
        {
            if(wishlist.equals("0"))
            {
                featuredView.img_wishlist.setColorFilter(Color.argb(255, 211, 211, 211));
                featuredView.txt_count.setText("0");
            }
            else if(wishlist.equals("1"))
            {
                featuredView.img_wishlist.setColorFilter(Color.argb(255,255,0,0));
                featuredView.txt_count.setText("1");
            }
        }
        int position=featuredView.getAdapterPosition();
        if(mSparseBooleanArray.get(position))
        {
            String value = featuredView.txt_count.getText().toString();
            if (value.equals("0")) {
                featuredView.img_wishlist.setColorFilter(Color.argb(255, 255, 0, 0));

                // save the selected state
            }
            else if (value.equals("1")) {
                featuredView.img_wishlist.setColorFilter(Color.argb(255,211,211,211));
                // save the selected state
            }
        }

    }

    @Override
    public int getItemCount() {
        return featured_data.size();
    }


    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    class FeaturedView extends RecyclerView.ViewHolder {
        ImageView img_brand,img_wishlist;
        TextView txt_brandname,txt_brand_price,txt_brand_item,txt_count;

        public ImageView getImg_brand() {
            return img_brand;
        }

        public ImageView getImg_wishlist() {
            return img_wishlist;
        }

        public TextView getTxt_brandname() {
            return txt_brandname;
        }

        public TextView getTxt_brand_price() {
            return txt_brand_price;
        }

        public TextView getTxt_brand_item() {
            return txt_brand_item;
        }

        public TextView getTxt_count() {
            return txt_count;
        }

        FeaturedView(@NonNull View itemView) {
            super(itemView);
            img_brand=itemView.findViewById(R.id.img_brand);
            img_wishlist=itemView.findViewById(R.id.img_wishlist);
            txt_brandname=itemView.findViewById(R.id.txt_brandname);
            txt_brand_price=itemView.findViewById(R.id.txt_brand_price);
            txt_brand_item=itemView.findViewById(R.id.txt_brand_item);
            txt_count=itemView.findViewById(R.id.txt_count);
            img_wishlist.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String value = txt_count.getText().toString();
                    if (value.equals("0")) {
                        img_wishlist.setColorFilter(Color.argb(255, 255, 0, 0));
                        txt_count.setText("1");

                    } else if (value.equals("1")) {
                        img_wishlist.setColorFilter(Color.argb(255,211,211,211));
                        txt_count.setText("0");


                    }
                    onclickListener.wishlistOnclick(v,getAdapterPosition());
                    mSparseBooleanArray.put(getAdapterPosition(), true);
                    notifyDataSetChanged();

                }
            });
            img_brand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onclickListener.brandOnClick(v,getAdapterPosition());
                }
            });
        }
    }

    private MyAdapterListener onclickListener;

    public interface MyAdapterListener
    {
        void wishlistOnclick(View view ,int position);
        void brandOnClick(View view,int position);
    }
}

https://jsfiddle.net/zmr7835e/