首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不更改在iframe中加载的输入按钮的背景色。

不更改在iframe中加载的输入按钮的背景色。
EN

Stack Overflow用户
提问于 2016-10-25 06:18:22
回答 1查看 417关注 0票数 0

我在我的项目中使用material-design。我将html文件加载到iframe中.我的html文件是

代码语言:javascript
复制
<form action="#" method="post" name="sign up for beta form">
    <div class="header">
        <p id="sampleTitle" contenteditable="true">Sign Up For Beta</p>
    </div>
    <div class="description">
        <p contenteditable="true">Milkshake is almost ready. If you're interested in testing it out, then sign up below
        to get exclusive access.</p>
    </div>
    <div class="input">
        <input type="text" class="button" id="email" name="email" placeholder="NAME@EXAMPLE.COM">
        <input type="submit" class="button" id="submit" value="SIGN UP">
    </div>
</form>

我必须根据用户的选择改变按钮的背景色。为此,我使用"jquery.minicolors.js“并将我的脚本写成.

代码语言:javascript
复制
<script type="text/javascript">
    // background button color
    $(document.body).on('change', '#hue-demo-btn-bg', function () {
        var val = $(this).val();
        console.log($(document).find('#submit'));
        $(document).find('#submit').attr('style', 'background-color: ' + val + ' !important');
    });
</script>

它不会更改按钮的background color,也会将空的object打印为

对象

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-25 06:29:18

因此,您的表单加载了iframe,您必须使用jQuery('#PLACE_IFRAME_ID').contents()选择器来获取iframe内容。

若要更改提交按钮的背景色,请使用下面的代码。

代码语言:javascript
复制
jQuery('#PLACE_IFRAME_ID').contents().find('#submit').attr('style', 'background-color: ' + val + ' !important');

注意:代替#PLACE_IFRAME_ID,将您的iframe's id放在选择器中。

请看一下工作演示:

代码语言:javascript
复制
// background button color
$(document.body).on('change', '#hue-demo-btn-bg', function () {
  var val = $(this).val();  
  jQuery('#IframeID').contents().find('#submit').attr('style', 'background-color: ' + val + ' !important');
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ifrmae id="IframeID">
  <form action="#" method="post" name="sign up for beta form">
    <div class="header">
      <p id="sampleTitle" contenteditable="true">Sign Up For Beta</p>
    </div>
    <div class="description">
      <p contenteditable="true">Milkshake is almost ready. If you're interested in testing it out, then sign up below
        to get exclusive access.</p>
    </div>
    <div class="input">
      <input type="text" class="button" id="email" name="email" placeholder="NAME@EXAMPLE.COM">
      <input type="submit" class="button" id="submit" value="SIGN UP">
    </div>
  </form>  
  </iframe>

<select id="hue-demo-btn-bg">
  <option value="">select</option>
  <option value="red">red</option>
  <option value="orange">Orange</option>
  <option value="blue">Blue</option>
</select>

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

https://stackoverflow.com/questions/40232659

复制
相关文章

相似问题

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