首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >仅保留客户端控件id

仅保留客户端控件id
EN

Stack Overflow用户
提问于 2011-05-13 22:17:52
回答 2查看 147关注 0票数 0

我有一种情况,我有许多对象是在后台代码中动态创建的。每个对象都有多个隐藏字段和一个图像按钮。

当图像按钮滚动时,我有一个js文件,它应该获得该对象的相关字段并做一些事情。

我遇到的问题是,当我将鼠标悬停在按钮上时,我得到一个空引用,因为getEmelementById不知道我引用的是哪个对象。

下面是ascx页面:

代码语言:javascript
复制
    <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>    

这是它在渲染时的外观。在这个例子中有两个:

代码语言:javascript
复制
        <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

代码语言: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?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-13 22:49:40

这是我的一般性建议。我假设您也在使用jQuery。

首先,当您要从JavaScript访问服务器端控件时,设置ClientIDs' mode to static总是很有帮助的。

其次,为什么您的数据被传递到隐藏字段?是否修改并回传?如果不是,则使用data-attributeName="value"形式的属性来存储数据的HTML5标准。

这是你的.ASCX页面中的超文本标记语言:

代码语言:javascript
复制
<div id="hfNarrative" class="narratives" runat="server">
   <!-- Stuff specific to this object, image, buttons, whatever -->
</div>

在.NET端,您可以调用:

代码语言:javascript
复制
hfNarrative.Attributes.Add("data-narrativeDate", SOME_DATE);
hfNarrative.Attributes.Add("data-narrativeBy", SOME_GUY);

然后,使用jQuery,您可以简单地访问以下内容:

代码语言:javascript
复制
$('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.
});

我希望这能帮助你走上一条更干净的道路。

票数 1
EN

Stack Overflow用户

发布于 2011-05-13 22:32:40

使用jQuery:

代码语言:javascript
复制
$('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数组

不得不说,这些名字和身份证太可怕了。如果可能,你应该让它们更具上下文、语义性和相关性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5993268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档