我有一种情况,我有许多对象是在后台代码中动态创建的。每个对象都有多个隐藏字段和一个图像按钮。
当图像按钮滚动时,我有一个js文件,它应该获得该对象的相关字段并做一些事情。
我遇到的问题是,当我将鼠标悬停在按钮上时,我得到一个空引用,因为getEmelementById不知道我引用的是哪个对象。
下面是ascx页面:
<div style="position:relative;float:right;" visible="true">
<asp:ImageButton ID="iconNarrative" visible="true" ImageUrl="/_layouts/images/osys/NarrativeIcon.gif" runat="server" Style="position:absolute; top:-28px; left:-25px; display:block;" onmouseout="hideNarrative()" onmouseover="showNarrative(this, document.getElementById(id))" />
<asp:HiddenField ID="hfNarrative" runat="server" Visible="true" />
</div> 这是它在渲染时的外观。在这个例子中有两个:
和
<div style="position:relative;float:right;" visible="true">
<input type="image" name="ctl00$m$g_90cae966_b430_4d11_8992_816553676250$ctl00$iconNarrative" id="ctl00_m_g_90cae966_b430_4d11_8992_816553676250_ctl00_iconNarrative" onmouseout="hideNarrative()" onmouseover="showNarrative(this, document.getElementById(id))" src="/_layouts/images/osys/NarrativeIcon.gif" style="border-width:0px;position:absolute; top:-28px; left:-25px; display:block;" />
<input type="hidden" name="ctl00$m$g_90cae966_b430_4d11_8992_816553676250$ctl00$hfNarrative" id="ctl00_m_g_90cae966_b430_4d11_8992_816553676250_ctl00_hfNarrative" />
<input type="hidden" name="ctl00$m$g_90cae966_b430_4d11_8992_816553676250$ctl00$hfNarrativeDate" id="ctl00_m_g_90cae966_b430_4d11_8992_816553676250_ctl00_hfNarrativeDate" />
<input type="hidden" name="ctl00$m$g_90cae966_b430_4d11_8992_816553676250$ctl00$hfNarrativeBy" id="ctl00_m_g_90cae966_b430_4d11_8992_816553676250_ctl00_hfNarrativeBy" />
</div> 这是我的javascript
var narrativeContainer = document.getElementById('narrative_container');
var narrative = document.getElementById('<%=hfNarrative.ClientID%>');
var narrativeBy = document.getElementById('<%=hfNarrativeBy.ClientID%>');
var narrativeDate = document.getElementById('<%=hfNarrativeDate.ClientID%>');
narrative = document.getElementById('<%=hfNarrative.ClientID%>');那么我现在该如何调用哪组隐藏字段呢?我唯一能想到做的事情就是获取Id并将其附加到各个字段,然后使用getElementById?
发布于 2011-05-13 22:49:40
这是我的一般性建议。我假设您也在使用jQuery。
首先,当您要从JavaScript访问服务器端控件时,设置ClientIDs' mode to static总是很有帮助的。
其次,为什么您的数据被传递到隐藏字段?是否修改并回传?如果不是,则使用data-attributeName="value"形式的属性来存储数据的HTML5标准。
这是你的.ASCX页面中的超文本标记语言:
<div id="hfNarrative" class="narratives" runat="server">
<!-- Stuff specific to this object, image, buttons, whatever -->
</div>在.NET端,您可以调用:
hfNarrative.Attributes.Add("data-narrativeDate", SOME_DATE);
hfNarrative.Attributes.Add("data-narrativeBy", SOME_GUY);然后,使用jQuery,您可以简单地访问以下内容:
$('div.narratives').each(function() {
var $this = $(this);
var narrBy = $this.attr('data-narrativeBy');
var narrDat = $this.attr('data-narrativeDate');
//Do stuff here with the data.
});我希望这能帮助你走上一条更干净的道路。
发布于 2011-05-13 22:32:40
使用jQuery:
$('input[type=image]).hover(function() {
var myElemID = $(this).attr('id'), inputJSON = {};
$(this).parent().children('input').each(function(i) {
inputJSON[$(this).attr('name')] = $(this).attr('id');
});
});这将为您提供一个带有图像输入ID的变量myElemID和一个inputJSON = {'ctl00$m$g_90cae966_b430_4d11_8992_816553676250$ctl00$hfNarrativeBy':'ctl00_m_g_90cae966_b430_4d11_8992_816553676250_ctl00_hfNarrativeBy'}的JSON数组
不得不说,这些名字和身份证太可怕了。如果可能,你应该让它们更具上下文、语义性和相关性。
https://stackoverflow.com/questions/5993268
复制相似问题