Почему текстовое оформление не работает в моем коде?

В Java все переменные, которые вы объявляете, на самом деле являются «ссылками» на объекты (или примитивы), а не самими объектами.

При попытке выполнить один метод объекта , ссылка просит живой объект выполнить этот метод. Но если ссылка ссылается на NULL (ничего, нуль, void, nada), то нет способа, которым метод будет выполнен. Тогда runtime сообщит вам об этом, выбросив исключение NullPointerException.

Ваша ссылка «указывает» на нуль, таким образом, «Null -> Pointer».

Объект живет в памяти виртуальной машины пространство и единственный способ доступа к нему - использовать ссылки this. Возьмем этот пример:

public class Some {
    private int id;
    public int getId(){
        return this.id;
    }
    public setId( int newId ) {
        this.id = newId;
    }
}

И в другом месте вашего кода:

Some reference = new Some();    // Point to a new object of type Some()
Some otherReference = null;     // Initiallly this points to NULL

reference.setId( 1 );           // Execute setId method, now private var id is 1

System.out.println( reference.getId() ); // Prints 1 to the console

otherReference = reference      // Now they both point to the only object.

reference = null;               // "reference" now point to null.

// But "otherReference" still point to the "real" object so this print 1 too...
System.out.println( otherReference.getId() );

// Guess what will happen
System.out.println( reference.getId() ); // :S Throws NullPointerException because "reference" is pointing to NULL remember...

Это важно знать - когда больше нет ссылок на объект (в пример выше, когда reference и otherReference оба указывают на null), тогда объект «недоступен». Мы не можем работать с ним, поэтому этот объект готов к сбору мусора, и в какой-то момент VM освободит память, используемую этим объектом, и выделит другую.

1
задан Temani Afif 14 April 2019 в 11:42
поделиться

2 ответа

Настройте эти CSS, вам нужно добавить CSS для .dropdown-content a и переместить a:hover в конец

.dropdown-content a{
  text-decoration: none ;
  }

  a:hover {
  text-decoration: underline ;
}

<!DOCTYPE html>

<html>
<head>
<style>
  ul {
  list-style-type:none;
}



.dropdown {
  display: inline-block;
  width:100px;
  height:25px;
  background-color: black;
  color: white;
  padding: 20px;
  text-align: center;
  font-size: 20px;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  padding: 12px 16px;
  text-decoration: none ;
 }
 
 .dropdown-content a{
  text-decoration: none ;
  }
  
  a:hover {
  text-decoration: underline ;
}
 

.dropdown:hover .dropdown-content {display: block;}
</style>
</head>
<body>


<ul>
<div class="dropdown">
  <li>list item 1</li>
    <div class="dropdown-content">
    <a href="" target="_blank">link 1</a><br>
    <a href="" target="_blank">link 2</a>
    </div>
</div>

<div class="dropdown">
  <li>list item 2</li>
  <a class="dropdown-content" href="" target="_blank">link 1</a>
</div>

 </body>
</html>

0
ответ дан Hien Nguyen 14 April 2019 в 11:42
поделиться

Вам нужно настроить таргетинг на элемент a, чтобы вы могли использовать дополнительную обертку в обеих ситуациях и настроить CSS, как показано ниже:

Я также исправил недопустимый HTML, поскольку вам разрешено используйте li только в качестве дочернего элемента ul

ul {
  list-style-type: none;
}

.dropdown {
  display: inline-block;
  width: 100px;
  height: 25px;
  background-color: black;
  color: white;
  padding: 20px;
  text-align: center;
  font-size: 20px;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  padding: 12px 16px;
}

.dropdown-content a {
  text-decoration: none;
}

.dropdown-content a:hover {
  text-decoration: underline;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<ul>
  <li class="dropdown">list item 1
    <div class="dropdown-content">
      <a href="" target="_blank">link 1</a><br>
      <a href="" target="_blank">link 2</a>
    </div>
  </li>

  <li class="dropdown">list item 2
    <div class="dropdown-content">
      <a href="" target="_blank">link 1</a>
    </div>
  </li>
</ul>

0
ответ дан Temani Afif 14 April 2019 в 11:42
поделиться