如何使用jQuery将类名添加到对象属性的数组列表中?

时间:2016-04-13 02:12:24

标签: javascript jquery

我正在尝试获取每个td的类名,然后为每个td添加另一个类。例如:当前类为class = "X-marker",我想添加另一个类bgreen,它应该是class = "X-marker bgreen"

我使用each方法获取每个td的类名,然后使用attr来设置它们,但它不起作用。我没有收到任何错误,但它没有按预期工作。

该对象名为winInfo,数组变量为:winInfo.play = winArray[i];

var winInfo = checkWin();
if (winInfo.win) {
    currentClass = $(winInfo.play).each(function () {
        $(this).attr('class');
    });

    $(winInfo.play).each(function () {
        $(this).attr('class', currentClass + ' bgreen');
    });
}

2 个答案:

答案 0 :(得分:2)

听起来你正在寻找addClass()

可能看起来像这样:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());

            JPanel textPane = new JPanel(new GridBagLayout());
            JTextField textField = new JTextField(20);

            textPane.add(new JLabel("Filename:"));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            textPane.add(textField, gbc);
            textPane.add(new JButton("Load"));

            JPanel buttonPane = new JPanel();
            buttonPane.add(new JButton("Play"));
            buttonPane.add(new JButton("Stop"));

            JPanel southPane = new JPanel(new GridLayout(2, 0));
            southPane.add(textPane);
            southPane.add(buttonPane);

            add(new JLabel("I'm in the center"));
            add(southPane, BorderLayout.SOUTH);
        }

    }

}

答案 1 :(得分:1)

如@Hatchet所述,方法addClass显然是最简单的方法。 但是,由于您似乎误解了each的工作原理,因此以下是使用eachaddClass进行此操作的方法。

我假设winInfo.play是一个合适的dom选择器(例如".class-name p")。

var winInfo = checkWin();
if (winInfo.win) {
    // `each` is just a helper that will call the provided function
    // for each of the object selected by jQuery (like a loop).
    // It does not return any value.
    $(winInfo.play).each(function () {
        var currentClass = $(this).attr('class');
        $(this).attr('class', currentClass + ' bgreen');
    });
}

哦,因为you might not need jQuery,这就是你在普通js中的表现方式。

var winInfo = checkWin();
if (winInfo.win) {
  // if winInfo.play is not a selector but a list of dom elements,
  // just remove document.querySelectorAll.
  for(var el of document.querySelectorAll(winInfo.play)){
    el.classList.add('bggreen');
  }
}