我怎样才能访问第一个标签?

时间:2016-12-09 14:58:14

标签: css

如何才能访问第一个标签?我试过

.form-group label:nth-child(1){
  margin-top: 4%;
}

然而我没有结果:\

<form class="login" method="post" action="/users/login">
      <div class="form-group">
        <label>Username</label>
        <input type="text" class="form-control" name="username" placeholder="Username">
      </div>
      <div class="form-group">
        <label>Password</label>
        <input type="password" class="form-control" name="password" placeholder="Password">
      </div>
      <button type="submit" class="btn btn-default">Submit</button>
   </form>

5 个答案:

答案 0 :(得分:2)

.form-group:first-child label{
  margin-top: 4%;
}

答案 1 :(得分:1)

访问第一个表单组,然后访问其标签:

.form-group:first-child > label {
    display: inline-block;
    margin-top: 4%;
}

编辑:假设您只想定位&#34;用户名&#34;的标签。

答案 2 :(得分:0)

修:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

class SetNumlockKeyOn
{
    [StructLayout(LayoutKind.Sequential)]
    public struct INPUT
    {
        internal int type;
        internal short wVk;
        internal short wScan;
        internal int dwFlags;
        internal int time;
        internal IntPtr dwExtraInfo;
        int dummy1;
        int dummy2;
        internal int type1;
        internal short wVk1;
        internal short wScan1;
        internal int dwFlags1;
        internal int time1;
        internal IntPtr dwExtraInfo1;
        int dummy3;
        int dummy4;
    }
    [DllImport("user32.dll")]
    static extern int SendInput(uint nInputs, IntPtr pInputs, int cbSize);

    public static void SetNumlockOn()
    {
        if (Control.IsKeyLocked(Keys.NumLock)) return;
        const int mouseInpSize = 28;//Hardcoded size of the MOUSEINPUT tag !!!
        INPUT input = new INPUT();
        input.type = 0x01; //INPUT_KEYBOARD
        input.wVk = 0x90; //VK_NUMLOCK
        input.wScan = 0;
        input.dwFlags = 0; //key-down
        input.time = 0;
        input.dwExtraInfo = IntPtr.Zero;

        input.type1 = 0x01;
        input.wVk1 = 0x90;
        input.wScan1 = 0;
        input.dwFlags1 = 2; //key-up
        input.time1 = 0;
        input.dwExtraInfo1 = IntPtr.Zero;

        IntPtr pI = Marshal.AllocHGlobal(mouseInpSize * 2);
        Marshal.StructureToPtr(input, pI, false);
        int result = SendInput(2, pI, mouseInpSize); //Hardcoded size of the MOUSEINPUT tag !!!

        //if (result == 0 || Marshal.GetLastWin32Error() != 0)
        // Console.WriteLine(Marshal.GetLastWin32Error());
        Marshal.FreeHGlobal(pI);
    }
}

答案 3 :(得分:0)

这是因为您的css代表每个.form-group元素中的 every-first (意为每个)标签。
您需要将其更改为第一个.form-group标签。

.form-group:first-child label {
 //code
}

答案 4 :(得分:0)

使用:nth-child是选择具有相同父元素的元素。简单来说,假设您有如下HTML:

<div>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
</div>

span:nth-child(1)div的第一个孩子,标记名为span。但是,如果您使用下面的HTML应用相同的选择器(就像您的情况一样):

<div>
    <span></span>
</div>
<div>
    <span></span>
</div>

所选元素均为span,因为span在父项中排在第一位div