从iframe访问父页面的id元素(accessing id element of parent page from iframe)

如何从iframe访问id元素到父页面? 我试图使用这种方法,但仍然无法正常工作。

Parent.html

<html>
    <head>
        <title>Parent</title>
    </head>
    <body> 
    <button id="click" onclick="alert('Hello');">click</button>
        <iframe id="iframe1" src="iframe.html">
        </iframe>
    </body>
</html>

Iframe.html的

<!DOCTYPE html>
<html>
    <head>
        <title>Parent</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-3.1.0.slim.js"></script>
<script>
$(function() {
    var money = 20000;

    $("#nominal").on("change keyup", function() {
        var input = $(this);

        // remove possible existing message
        if( input.next().is("form") )
            input.next().remove();

        // show message
        if( input.val() > money )
            window.parent.$('#click').trigger('click');
    });
});
</script>
    </head>
    <body> 
    <input type="text" id="nominal">
    </body>
</html>

我创建的脚本是否有问题? 请帮我改进这段代码。 提前致谢。

问题解决了


How can I access id element from iframe to parent page? I have tried to use this method but still not work.

Parent.html

<html>
    <head>
        <title>Parent</title>
    </head>
    <body> 
    <button id="click" onclick="alert('Hello');">click</button>
        <iframe id="iframe1" src="iframe.html">
        </iframe>
    </body>
</html>

iframe.html

<!DOCTYPE html>
<html>
    <head>
        <title>Parent</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-3.1.0.slim.js"></script>
<script>
$(function() {
    var money = 20000;

    $("#nominal").on("change keyup", function() {
        var input = $(this);

        // remove possible existing message
        if( input.next().is("form") )
            input.next().remove();

        // show message
        if( input.val() > money )
            window.parent.$('#click').trigger('click');
    });
});
</script>
    </head>
    <body> 
    <input type="text" id="nominal">
    </body>
</html>

Whether there is something wrong with the script I created? Please help me to improve this code. Thanks in advance.

PROBLEM SOLVED


原文:https://stackoverflow.com/questions/38517879
2024-05-07 19:05

满意答案

好的,我是如何解决它的,感谢所有回复,我正在寻找一种方法将事件附加到在运行时(初始化之后)动态创建的按钮。 希望它也适用于其他人。

<script type="text/javascript">
    function ButtonClick(buttonId) {
        alert("Button " + buttonId + " clicked from javascript");
    }
</script> 

protected void Button_Click(object sender, EventArgs e)
{
    ClientScript.RegisterClientScriptBlock(this.GetType(), ((Button)sender).ID, "<script>alert('Button_Click');</script>");
    Response.Write(DateTime.Now.ToString() + ": " + ((Button)sender).ID + " was clicked");
}    

private Button GetButton(string id, string name)
{
    Button b = new Button();
    b.Text = name;
    b.ID = id;
    b.Click += new EventHandler(Button_Click);
    b.OnClientClick = "ButtonClick('" + b.ClientID + "')";
    return b;
}

Ok here is how I solved it, Thanks for all replies, I was looking for a way to attach an event to a button that is created dynamically during runtime (after initialization). Hope It works for others as well.

<script type="text/javascript">
    function ButtonClick(buttonId) {
        alert("Button " + buttonId + " clicked from javascript");
    }
</script> 

protected void Button_Click(object sender, EventArgs e)
{
    ClientScript.RegisterClientScriptBlock(this.GetType(), ((Button)sender).ID, "<script>alert('Button_Click');</script>");
    Response.Write(DateTime.Now.ToString() + ": " + ((Button)sender).ID + " was clicked");
}    

private Button GetButton(string id, string name)
{
    Button b = new Button();
    b.Text = name;
    b.ID = id;
    b.Click += new EventHandler(Button_Click);
    b.OnClientClick = "ButtonClick('" + b.ClientID + "')";
    return b;
}

相关问答

更多

jQuery - 动态创建按钮并附加事件处理程序(jQuery - Dynamically Create Button and Attach Event Handler)

调用.html()将元素序列化为一个字符串,所有事件处理程序和其他相关数据都将丢失。 以下是我的做法: $("#myButton").click(function () { var test = $('<button/>', { text: 'Test', click: function () { alert('hi'); } }); var parent = $('<tr><td></td></tr>').children().a...

SharePoint:CompositeField在预渲染事件期间抛出NullReferenceException(SharePoint:CompositeField throws NullReferenceException during pre-render event)

最初我放弃了使用SharePoint的整个想法:CompositeField和类似的控件。 我回去使用基本的ASP.Net控件。 除处理附件外,这个工作正常。 但我无法弄清楚如何处理附件,但在我对解决方案的研究中,我注意到我可以恢复使用默认的SharePoint列表表单。 这就是我尝试过的,但后来我遇到了查找字段的问题。 我无法让他们使用声明性XML,必须在功能激活期间创建字段。 这又导致了场序列的问题。 查找字段全部出现在表单的末尾,而不是它们在其余字段中的适当位置。 我尝试使用FieldLin...

以编程方式在SharePoint中创建asp:Button和attach事件(Programmatically create asp:Button and attach event in SharePoint)

好的,我是如何解决它的,感谢所有回复,我正在寻找一种方法将事件附加到在运行时(初始化之后)动态创建的按钮。 希望它也适用于其他人。 <script type="text/javascript"> function ButtonClick(buttonId) { alert("Button " + buttonId + " clicked from javascript"); } </script> protected void Button_Click(obje...

Sharepoint WebPart(Sharepoint WebPart)

我会读到这个: http : //msdn.microsoft.com/en-us/library/ms452873.aspx 它将回答您关于创建Web部件,部署等的许多问题。 I would read though this: http://msdn.microsoft.com/en-us/library/ms452873.aspx It will answer a lot of your questions about creating a web part, deploying etc.

事件接收器sharepoint在线(event receiver sharepoint online)

我自己从未这样做,但我相信你需要安装SharePoint在线工具。 然后,您需要执行一些powershell命令然后删除。 # Paths to SDK. Please verify location on your computer. Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" A...

在sharepoint中重复事件(Repeating event in sharepoint)

你可以用Recurrence创建1个事件。 选择每周选项 每1周重复一次 选择星期日和星期一 开课日期:9月1日 结束时间:10月31日 但如果时间段不在行(例如:9月和11月),则需要在每个月创建2个重复事件。 you can just create 1 event with Recurrence. Choose weekly option Recur every 1 week Select Sunday and Monday Start Date : 1 September End By : ...

ASP.NET以编程方式添加带事件的按钮(ASP.NET Programmatically adding button with an event)

您应该在Page的Init事件处理程序中添加动态控件,以便适当地触发ViewState和Events。 试着这样做: protected void Page_Init(object sender, EventArgs e) { Button btn = new Button(); btn.ID = "btnTest"; btn.Text = "I was dynamical...

SharePoint安全编程问题(SharePoint security programming issue)

执行这些命令的用户是否有权运行这些命令? 根据文档 ,当您将该属性设置为false时,似乎禁用了安全验证。 获得“超级用户”权限以执行当前用户无权运行的命令的更好方法是使用SPSecurity.RunWithElevatedPrivileges SPSecurity.RunWithElevatedPrivileges(delegate() { // Note: It's important that you create all new SPSite and SPWeb // o...

SharePoint 2010:创建“提醒我”按钮(SharePoint 2010: Create “Alert Me” button)

对于那些有兴趣的人,我最终为此推出了自己的用户控件。 代码如下: HTML <asp:HyperLink ID="AlertHyperLink" runat="server"><img alt="Alert me" src="/_layouts/images/menualert.gif" title="Alert me to any changes that get made to this site." /></asp:HyperLink> C# protected void Page_Pre...

asp.net onclick事件没有被解雇(asp.net onclick event not fired)

这是我找到的解决方案。 我仍然不知道为什么事件消失了,但我注意到只有当我从管理面板显示webpart时才会触发它们(站点操作>设置> webpart库>单击我的自定义webpart)。 如果我将我的webpart添加到测试页面,一切正常。 如果其他人有同样的问题,我希望他会读到这个并避免我失去的那一周...... 谢谢大家的时间和答案:) here comes the solution I've found. I still don't know why the event disappeared...

相关文章

更多

如何向一个页面中的两个iframe传值

现在有一个页面上面是一个iframe的报表 下面是一个iframe的图形 分别向两个iframe传 ...

如何获取iframe刷新后的内容

我在做一个基于iframe的文件上传,然后服务器写回一个JSON数据输出到隐藏的iframe里面,那么 ...

div 与 iframe页面缩小问题 [JavaScript]

&lt;div width=&quot;20%&quot;&gt;&lt;/div&gt; &lt; ...

js 通过td的id值 如何拿到tr的id值?

有以下代码:&lt;tr id=&quot;bb&quot;&gt;&lt;td id=&quot;a ...

Stack Overflow Architecture Update - Now At 95 Million Page Views A Month

A lot has happened since my first article on theSta ...

使用TabPanel时,如果两个页面存在相同的id。

我左边是一颗tree,右边是TabPanel。当点击一个结点 A,autoload一个页面 A.jsp ...

MongoDB _id和ObjectId详解

在创建一个文档的时候,会生成一个_id,id的默认类型是ObjectId,如: &gt; db. ...

solr required field: id

为了和以前的程序兼容,在solr建立索引的时候,将id设为gid,结果在建立索引时候出现如下错误: o ...

The content of element type "package" must match "...

在编写后台登陆模块时,将许多默认的设置放在一个名为default的package 里。然后再定义其他 ...

html中一个div的id是“1:222”的话,怎么利用id给它定义css啊?

如 &lt;style&gt; #1:2{ height:100px; width:100px ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本