我试图使用JavaScript更改链接的颜色,它不起作用

时间:2018-01-12 08:21:34

标签: javascript

我使用了以下代码



<a id="link1" href= "http://www.yahoo.com" onmouseover="document.getElementById('link1').style.color='yellow'">this is a link</a>
&#13;
&#13;
&#13;

这是对的吗?我没有得到输出

2 个答案:

答案 0 :(得分:2)

要更改css,您有不同的解决方案:

第一名:

您可以在css文件中为div添加悬停:

a:hover { color: blue; }

第二名:

您可以在js文件中创建自定义JS函数:

$("a").hover(function() {
  $(this).css("background-color","blue")
});

第3名:

您可以直接在div中添加方法:

<a id="link1" href= "http://www.yahoo.com" onmouseover="this.style.background='#FFFF99';>this is a link</a>

如果你在div上添加方法,你不必调用getElementById,你已经在元素上了

答案 1 :(得分:0)

&#13;
&#13;
public class XMLoader {
private final String XML_PATH = "src\\main\\java\\products.xml";
private List<SearchData> data = new ArrayList<SearchData>();

public XMLoader() throws ParserConfigurationException, IOException, SAXException {
    File inputFile = new File(XML_PATH);
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();

    Document document = documentBuilder.parse(inputFile);
    NodeList nodeList = document.getDocumentElement().getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            String id = node.getAttributes().getNamedItem("ID").getNodeValue();
            String searchedProduct = element.getElementsByTagName("Category").item(0).getChildNodes().item(0).getNodeValue();
            data.add(new SearchData(id, searchedProduct));
        }
    }
}

public class SearchData {
    private String id;
    private String searchedProduct;


    public SearchData( String id, String searchedProduct) {
        this.searchedProduct = searchedProduct;
        this.id = id;
    }

    public String getSearchedProduct() {
        return searchedProduct;
    }

    public String getId() {
        return id;
    }

    @Override
    public String toString() {
        return "SearchData{" +
                "id='" + id + '\'' +
                ", searchedProduct='" + searchedProduct + '\'' +
                '}';
    }
}
&#13;
&#13;
&#13;