dongren1986 2013-05-24 19:57
浏览 42

Javascript没有触发另一个函数

I am trying to add new elements (buttons) to html file using javascript. Here is the working script:

<div id="examples">
    <?php
    while ($row=mysql_fetch_array($result))
    {?>
    <script type="text/javascript">
        var element = document.createElement("input");
        element.type = "button";
        element.value = "Click me";
        element.id = <?=$row['id'];?>;

        var foo = document.getElementById("examples");
        foo.appendChild(element);
    </script>

    <?php
    }
    ?>
</div>

The script adding new elements to html file, but I would like to change some of the properties (for example:button size) too.

I have method SetProperties(id, fontSize) and if I am adding it in code when elements should be created it doesn't work, it even do not creates that new elements. Here is the code which makes me a headache:

<div id="examples">
    <?php
    while ($row=mysql_fetch_array($result))
    {?>
    <script type="text/javascript">
        var element = document.createElement("input");
        element.type = "button";
        element.value = "Click me";
        element.id = <?=$row['id'];?>;

        var foo = document.getElementById("examples");
        foo.appendChild(element);

        *SetProperties(<?=$row['id'];?>, <?=$row['fontSize'];?>);*
    </script>

    <?php
    }
    ?>
</div>

And here is the function which changes new elements properties:

<script type="text/javascript">
function SetProperties(id, fontSize)
{
    var btn = document.getElementById(id);
    btn.style.fontSize = fontSize + "px;";
}
</script>
  • 写回答

5条回答 默认 最新

  • duanliaolan6178 2013-05-24 20:29
    关注

    you may try like this

    <style>
    #examples [type=button]
    {
        font-size:25px;
    }
    </style>
    
    <div id="examples">
        <?php
        while ($row=mysql_fetch_array($result))
        {?>
        <script type="text/javascript">
            var element = document.createElement("input");
            element.type = "button";
            element.value = "Click me";
            element.id = <?=$row['id'];?>;
    
            var foo = document.getElementById("examples");
            foo.appendChild(element);
    
        </script>
    
        <?php
        }
        ?>
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示