查找包含属性一部分的元素

时间:2015-10-01 18:15:51

标签: javascript jquery jquery-selectors

我找到一个可以匹配以下内容的选择器时遇到了一些麻烦:

public class MainActivity extends FragmentActivity {

// Declare Variables
ViewPager viewPager;
PagerAdapter adapter;   
CirclePageIndicator mIndicator; 
private int mWidthScreen;
private int mHeightScreen;  
private Bundle bundle;
private List<Fragment> frgScreens;
private int selectedtheme;
private Handler mHandler = new Handler();
//public  mlayoutForeground;

//Menu buttons
private ImageView mBtnMenu;
private SlidingMenu menu;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the view from viewpager_main.xml
    setContentView(R.layout.viewpager_main);

    getScreenHeightWidhth();    
    buildargument();

    //setting the required theme
    this.selectedtheme=1;       
    initialisePaging(selectedtheme); //Page creating function

    generateMenu();



}

private void initialisePaging(int theme) {  

    // Locate the ViewPager in viewpager_main.xml
    viewPager = (ViewPager) findViewById(R.id.pager);

    //#FIXME try to implement like theme.basic
    //clearing old entries
    frgScreens=new Vector<Fragment>();

    if(theme==1)
    {
        frgScreens=this.basicThemes();
    }

    if(theme==2)
    {   

        frgScreens=this.forecastThemes();
    }


  //Bind result to ViewPagerAdapter 
    adapter =new ViewPagerAdapter(this.getSupportFragmentManager(), frgScreens);

    // Binds the Adapter to the ViewPager
    this.viewPager.setAdapter(adapter);     
    addAndConfigureCirclePagerAdapter();



}


/*
 * forecast theme fragment
 */
private List<Fragment> forecastThemes()
{
    Fragment frgForecast1=Fragment.instantiate(this,ForecastScreen1.class.getName());

    //#FIXME forecast2 problematic
    //Fragment frgForecast2=Fragment.instantiate(this,ForecastScreen2.class.getName());

    frgForecast1.setArguments(bundle);
    //frgForecast2.setArguments(bundle);


    List<Fragment> frgForecastScreens = new Vector<Fragment>();

    frgForecastScreens.add(frgForecast1);
    //frgForecastScreens.add(frgForecast2);

    return frgForecastScreens;

}
/*
 * Basic theme fragments
 */
private List<Fragment> basicThemes()
{
    //declaring fragments Group Basic
    Fragment frgBasic1=Fragment.instantiate(this,Basic1.class.getName());
    Fragment frgBasic2=Fragment.instantiate(this,Basic2.class.getName());
    Fragment frgBasic3=Fragment.instantiate(this,Basic3.class.getName());
    Fragment frgBasic4=Fragment.instantiate(this,Basic4.class.getName());

    //Passing arguments
    frgBasic1.setArguments(bundle);
    frgBasic2.setArguments(bundle);
    frgBasic3.setArguments(bundle);
    frgBasic4.setArguments(bundle);


    //Loading screens into Fragment list
    List<Fragment> frgBasicScreens = new Vector<Fragment>();

    frgBasicScreens.add(frgBasic1);
    frgBasicScreens.add(frgBasic2);
    frgBasicScreens.add(frgBasic3);
    frgBasicScreens.add(frgBasic4);


    return frgBasicScreens;

}


 /*
  * Applying circlepageradapter color
  */
 private void addAndConfigureCirclePagerAdapter()
 {

     this.viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                            super.onPageSelected(position);
                            if (position == 0) {
                                menu.removeIgnoredView(viewPager);
                            } else {
                                menu.addIgnoredView(viewPager);
                            }
                        }
                    });

    mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
    this.mIndicator.setBackgroundColor(Color.TRANSPARENT);
    this.mIndicator.setPadding(5, 5, 5, 5);
    this.mIndicator.setRadius(12);
    this.mIndicator.setFillColor(Color.argb(255, 175, 220, 243));
    final float density = getResources().getDisplayMetrics().density;
    this.mIndicator.setRadius(6 * density);
    this.mIndicator.setViewPager(viewPager); 


 }

/*
 * Function to get height and width
 * 
 */
private void getScreenHeightWidhth()
{
     DisplayMetrics localDisplayMetrics = new DisplayMetrics();
     getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);            
     this.mWidthScreen = localDisplayMetrics.widthPixels;
     this.mHeightScreen = localDisplayMetrics.heightPixels; 
     mHandler.post(new DisplayToast(this, "Width Screen:"+ mWidthScreen + ". Height Screen: "+ mHeightScreen+ "."));
}

/*
 * Bundle that bind extra arguments with every fragments
 * mWidthScreen : Putting 'mWidthScreen'as a pasing argument
 */
private void buildargument()
{
    this.bundle = new Bundle();
    bundle.putInt("mWidthScreen", this.mWidthScreen);
}

public void generateMenu()
{
    //enable button
    //buttonsOn();

    mBtnMenu = (ImageView) findViewById(R.id.btnMenu);

    menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.LEFT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    menu.setShadowWidth(5);
    menu.setFadeDegree(0.0f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    menu.setBehindWidth(500);
    menu.setMenu(R.layout.menu_frame);

    mBtnMenu.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
         // menu.showMenu();

          menu.toggle();
         mBtnMenu.setEnabled(true);
        }
    });





}

public void buttonsOff()
  {
    this.mBtnMenu.setEnabled(false);

  }

  public void buttonsOn()
  {

    this.mBtnMenu.setEnabled(true);
  }


 @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}

因此,如果我有$("input[data-rule-*]"); data-rule-required,则需要匹配这两个属性。 (仅举例,我有一堆后缀,指定所有这些都不是一个选项)。

我发现只有data-rule-email选择器,不符合我的条件。

我看了一下这个主题:how do I find elements that contain a data-* attribute matching a prefix using jquery。问题是一样的,但我想知道是否有更紧凑的解决方案。

HTML示例。假设我可以拥有更多具有不同$("#element[attribute=value]")属性的输入:

data-rule-something

2 个答案:

答案 0 :(得分:2)

您的选项是向元素添加其他属性或标识符,以便您可以全部选择它们,或者在查询选择器中包含所有属性组合。

下面的示例为两个data-object元素添加了一个额外的input单例,然后查询该属性以更新字段值。

&#13;
&#13;
var dataObjects = document.querySelectorAll("[data-object]");
for(var i = 0, len = dataObjects.length; i < len; i++){
  dataObjects[i].value = i;
}
&#13;
<input type="text" data-rule-required data-object />
<input type="text" data-rule-email data-object />
&#13;
&#13;
&#13;

答案 1 :(得分:1)

一种不那么优雅的方式,但似乎它正在起作用:

$("input").filter(function() {

   if(Object.keys($(this).data()).toString().indexOf('rule')!==-1) {

    return $(this);
   }
}).css('background-color','red');

演示:http://jsfiddle.net/8x05bh2v/