我有元素(<a>
標籤中的圖像)列表,它是按動態ID排列的。可以只選擇其中的一個。假設我選擇了任何元素。這一次,我改變了它的風格,並將「SelectedItem」值設置爲其名稱屬性以便將來查找所選項目。 我想,當鼠標懸停在選定的項目,改變其src值,但jquery功能不起作用,因爲我設置name =「SelectedItem」屬性與JavaScript,在運行時,它重新加載頁面後工作。當我在代碼中設置name =「SelectedItem」時(默認情況下,運行時),它工作。但我想在不重新加載頁面的情況下執行此操作。更改元素的屬性值後,jquery函數不工作
不久,當我做選擇操作,我設置的名字= 「的SelectedItem」 到所選圖像:
function SelectItem(id) {
var imgId = 'productImage_' + id;
var imgId_element = document.getElementById(imgId.toString());
//my some operations
imgId_element.name = "SelectedItem";
}
當取名字的值取消選擇。
function UnSelectItem(id) {
var imgId = 'productImage_' + id;
var imgId_element = document.getElementById(imgId.toString());
//my some operations
imgId_element.name = "";
}
我想將未選擇的圖像設置爲僅選定圖像的背景。當我Select/Unselect
任何項目SelectItem()/UnSelectItem()
以下mouseover()/mouseout()
不起作用。
$("img[name='SelectedItem']").mouseover(function() {
$(this).attr("src", "/Content/images/select.png");
}).mouseout(function() {
$(this).attr("src", "/Content/images/unselect.png");
});
HTML是:
<img src="@Url.Content("~/Content/images/select.png")" id='[email protected](Model.Id)' data-id='@Model.Id' @{if (Model.IsSelected) { <text>name="SelectedItem"</text>} } alt="" />
我怎樣才能解決這個問題?
將jQuery與純DOM操作混合在一起會讓人感到困惑。如果你打算使用jQuery,你可能會全押。 – Pointy
'imgId'是一個字符串,你不需要'.toString()' – Bergi