php - 通过 id 获取 div 值 - Javascript

标签 php javascript drupal html

我正在尝试将我的 div 值获取到 Javascript 中。这是一个 Drupal 网站,我想转换货币并在 javascript 弹出窗口中显示它们。

到目前为止我有这个:

<?php 
        print "<script type='text/javascript'>";
        $rate=currency_api_convert("HKD", "CNY");
        print "exchangerate = ".$rate["value"].";";

        print "var $displaycur = getElementById('record-price');";
        print "$displaycur;";

        print "</script>";
?>

我在浏览器中得到的是:

<script type="text/javascript">
        exchangerate = 0.8406;var  = getElementById('record-price');;
</script>

虽然我知道为什么会这样显示,但我仍然不知道如何在适当的变量中获取div 的ID。 有任何想法吗?我应该使用 innerHTML 吗?

更新: 部分源文件: page.tpl.php

<?php
// $Id: page.tpl.php,v 1.18.2.1 2009/04/30 00:13:31 goba Exp $
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php print $language->language ?>" lang="<?php print $language->language ?>" dir="<?php print $language->dir ?>">
  <head>
    <?php print $head ?>
    <title><?php print $head_title ?></title>
    <?php print $styles ?>
    <?php print $scripts ?>
    <?php 
        // lets get the exchange rate from HKD to RMB, with the help of yahoo finance api and the currency api module
        // we pass the value to javascript, the tooltip will handle the rest
        print "<script type='text/javascript'>";
        $rate=currency_api_convert("HKD", "CNY");
        print "exchangerate = ".$rate["value"].";";
        print "</script>";
    ?>
    <script type="text/javascript">
        function showCurrency() {
            alert('$rate');
        }
    </script>
    <!--[if lte IE 7]>
      <?php print phptemplate_get_ie_styles(); ?>
    <![endif]-->

        <!--[if IE 6]>
            <style type="text/css">
                .fg-menu-ipod .fg-menu li { width: 95%; }
                .fg-menu-ipod .ui-widget-content { border:0; }
                .views-field-field-performer-value { margin-left:-150px;}
                .views-field-field-instruments-value { margin-left:-150px;}
                .coda-nav{display:block; position:absolute; width:400px;height:20px;top:260px;right:100px;z-index:125421;}  </style>
        <![endif]-->


  </head>
  <body<?php print phptemplate_body_class($left, $right); ?>>

还有node-record.tpl.php:

print '<div id="part1-right-line3">';   
    print '<div id="record-price">';                                                        
    print uc_currency_format($node->sell_price);
    print '</div>';                     
    print '</div>'; 
    print '<div id="part1-right-line4">';   
    print '<div id="add-to-cart">';     
    print drupal_get_form('uc_product_add_to_cart_form_'. $node->nid, $node);
    print '</div>'; 
    print '</div>';
    print '</div>';

这是输出: enter image description here 如果您需要更多,请告诉我,因为有 :) 提前致谢

最佳答案

<?php 
   print "<script type='text/javascript'>\n";
   $rate=currency_api_convert("HKD", "CNY");
   print "exchangerate = ".$rate["value"].";\n";
   print "var displaycur = document.getElementById('record-price');\n";
   print "alert(displaycur.innerHTML);\n";
   print "</script>\n";
?>

我想 'record-price' 是您的 DIV 的 ID...所以 displaycur.innerHTML将拥有 <div> 之间的所有内容和 </div> .


编辑:

function showCurrency() {
  alert(document.getElementById('record-price').innerHTML);
  }

关于php - 通过 id 获取 div 值 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5643973/

相关文章:

mysql - 如何在 drupal 中限制某些用户访问内容?

php - 如何导出/导入 utf8-general-ci 数据库?

php - Laravel 5 : Password Reset - move bcrypt password to User. php 模型?

php - 将元素置于另一个元素的右侧

javascript - Zf2 布局 css 和 javascript 路径在子路由上不解析

JavaScript 代码未执行!

drupal - 如何从面包屑中删除HOME

javascript - 如何在单页应用程序中加载新脚本?

javascript - jqueryui - 如何获得最顶层对话框的位置?

drupal - Drupal 6 中允许不同用户类型注册吗?