В 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 освободит память, используемую этим объектом, и выделит другую.
Вы не нуждаетесь в шоу и скрываете теги:
$(document).ready(function(){
$('.expand').click(function() {
if( $(this).hasClass('hidden') )
$('img', this).attr("src", "plus.jpg");
else
$('img', this).attr("src", "minus.jpg");
$(this).toggleClass('hidden');
$(this).parent().next().toggle();
});
});
редактирование: Хорошо, я добавил код для изменения изображения. Это - всего один способ сделать это. Я добавил класс к расширять атрибуту как тег, когда строка, которая следует, скрыта и удалила его, когда строку показали.
Попробуйте это...
//this will bind the click event
//put this in a $(document).ready or something
$(".expand").click(expand_ClickEvent);
//this is your event handler
function expand_ClickEvent(){
//get the TR that you want to show/hide
var TR = $('.expand').parent().next();
//check its class
if (TR.hasClass('hide')){
TR.removeClass('hide'); //remove the hide class
TR.addClass('show'); //change it to the show class
TR.show(); //show the TR (you can use any jquery animation)
//change the image URL
//select the expand class and the img in it, then change its src attribute
$('.expand img').attr('src', 'minus.gif');
} else {
TR.removeClass('show'); //remove the show class
TR.addClass('hide'); //change it to the hide class
TR.hide(); //hide the TR (you can use any jquery animation)
//change the image URL
//select the expand class and the img in it, then change its src attribute
$('.expand img').attr('src', 'plus.gif');
}
}
Hope это помогает.
Это - то, как изображения настраиваются в Изменении html
<tr>
<td colspan="2" align="center"
<input type="image" src="save.gif" id="saveButton" name="saveButton"
style="visibility: collapse; display: none"
onclick="ToggleFunction(false)"/>
<input type="image" src="saveDisabled.jpg" id="saveButtonDisabled"
name="saveButton" style="visibility: collapse; display: inline"
onclick="ToggleFunction(true)"/>
</td>
</tr>
onClick
событие к Вашей собственной функции, которую это находится в JS для переключения между ними.
В
ToggleFunction(seeSaveButton){
if(seeSaveButton){
$("#saveButton").attr("disabled", true)
.attr("style", "visibility: collapse; display: none;");
$("#saveButtonDisabled").attr("disabled", true)
.attr("style", "display: inline;");
}
else {
$("#saveButton").attr("disabled", false)
.attr("style", "display: inline;");
$("#saveButtonDisabled")
.attr("disabled", true)
.attr("style", "visibility: collapse; display: none;");
}
}
Ни у кого нет любви к тернарному оператору?:) Я понимаю соображения удобочитаемости, но по некоторым причинам это нажимает, чтобы я записал это как:
$(document).ready( function () {
$(".expand").click(function() {
$("img",this).attr("src",
$("img",this)
.attr("src")=="minus.gif" ? "plus.gif" : "minus.gif"
);
$(this).parent().next().toggle();
});
});
... и обладает преимуществом никаких посторонних классов.