CSS高度样式的CSS属性选择器

时间:2016-12-11 05:10:52

标签: css css-selectors inline-styles

的选择器
public class LoginActivity extends AppCompatActivity {

    private static final String TAG = LoginActivity.class.getSimpleName();
    private static final int RC_SIGN_IN = 1;
    private GoogleApiClient googleClient;
    private CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_login);


        callbackManager = CallbackManager.Factory.create();

        SignInButton googleButton = (SignInButton)findViewById(R.id.google_button);
        LoginButton facebookBtn = (LoginButton)findViewById(R.id.fb_login_button);
        Button emailButton = (Button)findViewById(R.id.email_button);
        googleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleClient);
                startActivityForResult(signInIntent, RC_SIGN_IN);
            }
        });

        initGoogleSignIn();
        initFacebookSignIn(facebookBtn);
    }

    private boolean isLoggedInByFacebook(){
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        return accessToken != null;
    }

    private void initGoogleSignIn(){
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        googleClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    }
                })
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
        OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(googleClient);
        if (opr.isDone()) {
            // If the user's cached credentials are valid, the OptionalPendingResult will be "done" and the GoogleSignInResult will be available instantly.
            Log.d("TAG", "Got cached sign-in");
            GoogleSignInResult result = opr.get();
            finish();
        }

    }

    private void initFacebookSignIn(LoginButton facebookBtn){
        if(isLoggedInByFacebook()) {
            finish();
        }else{
            callbackManager = CallbackManager.Factory.create();
            facebookBtn.setReadPermissions(Arrays.asList(
                     "public_profile","email"));
            // Callback registration
            facebookBtn.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    loginResult.getAccessToken().getUserId();
                    // App code
                    GraphRequest request = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject object, GraphResponse response) {
                                    try {
                                        Log.i("Response",response.toString());
                                        String email = response.getJSONObject().getString("email");
                                        String name = response.getJSONObject().getString("name");
                                        finish();
                                    }catch (JSONException e){
                                        Log.e(TAG,"Error getting facebook email", e);
                                    }
                                }
                            });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "name,email");
                    request.setParameters(parameters);
                    request.executeAsync();
                }

                @Override
                public void onCancel() {
                    // App code
                }
                @Override
                public void onError(FacebookException exception) {
                    Log.e(TAG,"Error in facebook sign in", exception);
                }
            });
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                // Signed in successfully, show authenticated UI.
                GoogleSignInAccount acct = result.getSignInAccount();
                api.loginGoogle(acct.getIdToken()).subscribe(new Action1<User>() {
                    @Override
                    public void call(User user) {
                        api.getWeather(-31.0, 115.0).subscribe(new Action1<WeatherResponse>() {
                            @Override
                            public void call(WeatherResponse weatherResponse) {
                            }
                        });
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        System.out.println(throwable);
                    }
                });
            } else {
                System.out.println(result.getStatus());
            }
        }else { //facebook 
            callbackManager.onActivityResult(requestCode, resultCode, data);
        }
    }

}

与普通的CSS属性选择器相同,即 <tr style="height:64px"> tr[style="height:64px"]tr[style=height:64px]

我可能没有正确地尝试过,但上述情况都不适合我。

更新:

@torazaburo我接受@ balapa的答案不是因为分号,而是因为我的尝试都没有,但@balapa给我看了一个有效的代码。我相信没有分号它仍然可以工作,但这对我来说远不如拥有工作代码那么重要。

BTW,FTR,事实证明我的测试工具是问题的根源,之后我就写了a better tool to test CSS selection from command line。有了它,(Go)选择器应指定为tr[style="height\3a 64px"]

3 个答案:

答案 0 :(得分:2)

在符合规范的浏览器中,tr[style=height:64px]会抛出DOM错误,说这是一个无效的选择器。带双引号的版本有效。这是因为spec表示属性选择器中的值必须是CSS标识符或字符串height:64px不是标识符,因此失败。将其括在引号(单引号或双引号)中使其成为一个有效的字符串。

如果您不想引用它,则需要使用CSS标识符的转义机制来转义冒号。这就是[style=height\:64px]有效的原因(下面的第二个例子)。有关详细信息,请参阅this question,其中此问题基本上是重复的。

[style="height\:64px"]有效,因为转义冒号本质上是无操作。

[style="height\3a 64px"]按预期工作,包括或不包含引号。 \3a是冒号的CSS转义符,以下空格终止转义序列。我不知道为什么这对你不起作用。如果您将此选择器指定为JavaScript字符串,可能您忘记了反斜杠。

[style="height&#58;64px"]不起作用,因为字符引用仅在HTML中有意义。它们在CSS选择器中没有任何意义。

这与style属性值是否以分号结尾无关。因此,接受的答案提供的描述是错误的。它只能用于引号。

底线:只需将您的属性值包装在引号中,然后再担心它。

function test(sel) {
  try {
    console.log(sel, "yields", document.querySelector(sel));
  } catch (e) {
    console.log(sel, "fails with error", e.name);
  }
}

test('tr[style=height:64px]');
test('tr[style=height\\:64px]');
test('tr[style="height:64px"]');
test('tr[style="height\\:64px"]');
test('tr[style="height\\3a 64px"]');    
test('tr[style=height\\3a 64px]');
test('tr[style="height&#58;64px"]');
<table>
  <tr style="height:64px"></tr>
</table>

答案 1 :(得分:0)

这样做:

.trHeight { height: 64px} 

和代码:

<tr class='trHeight"></tr>

并通过

抓住它
".trHeight"

使用样式属性是一种危险的做法 CSS selector by inline style attribute

答案 2 :(得分:0)

您需要在HTML和CSS代码的结尾代码中添加分号。看看下面的例子

&#13;
&#13;
tr[style="height:64px;"] {
background: red;
}
&#13;
<table>
  <tr>
    <td>Regular TR</td>
  </tr>
  <tr style="height:64px;">
    <td>TR with inline style</td>
  </tr>
&#13;
&#13;
&#13;