【问题标题】:Changing text inside of an html <em> tag using jquery使用 jquery 更改 html <em> 标记内的文本
【发布时间】:2015-11-04 11:10:08
【问题描述】:

我只想更改标签内匹配的确切单词,而不是所有标签。 onload 是最好的方法吗?我尝试在 php 中使用 str_repace,但由于某种原因,它在一个页面上工作,但在我需要它工作的页面上却没有。

<?php 
     $var = "<em>Bicycle</em>";
     echo str_replace($var, "Bicycle", "(something different)");
    ?>

现在我已经尝试了一些不同的 html dom 方法和一些 javascript。

  <em>Bicycle</em>

<script>
document.getElementsByTagName("em").innerHTML = "(something different)";
</script>

<?php foreach ($this->components as $id => $comp): ?>
        <tr class="row">
            <td class="col1">
                <img src="<?php echo $this->baseUrl('images/' . $comp->img_src) ?>"
                     alt="<?php echo $this->translate(sprintf($comp->translation_key, 'name')) ?>">
            </td>
            <td class="col2">
                <?php echo $this->translate(sprintf($comp->translation_key, 'name')) ?>
                <em><?php echo $this->translate(sprintf($comp->translation_key, 'description')) ?></em>
            </td>
            <td class="col3">
                <?php echo $this->formCheckbox(
                    "insurance[$id]", '1', !empty($this->order['insurance'][$id]), array('class' => 'row-checkbox')
                ) ?>
                <label for="checkbox"><?php echo $this->translate(
                        'order_additional_insurance_checkbox_label'
                    ) ?></label>
            </td>
            <td class="col4"><?php echo $this->translate(sprintf($comp->translation_key, 'dimensions_metric')) ?>
                <em><?php echo $this->translate(sprintf($comp->translation_key, 'dimensions_customary')) ?></em>
            </td>
            <td class="col5">
                <span class="rent">$<?php echo $comp->price ?></span>
            </td>
            <td class="col6">
                <div class="field-holder counter-holder row-qty" data-max-val="99">
                    <?php echo $this->formText(
                        "components[{$id}][quantity]",
                        (int)$this->order['components'][$id]['quantity'],
                        array(
                            'class' => 'required-aggregate-quantity',
                            'id'    => "component_{$id}_quantity"
                        )
                    ) ?>
                    <a class="btn-top" href="#">top</a>
                    <a class="btn-bottom" href="#">bottom</a>
                </div>
            </td>
            <td class="col7">
                <?php
                $rowCost = (int)$this->order['components'][$id]['quantity'] * $comp->price;
                ?>
                <span class="cost">$<?php echo number_format($rowCost, 2) ?></span>
            </td>
        </tr>
    <?php endforeach; ?>

专注于翻译键“名称”。

【问题讨论】:

  • 如果你已经加载了 jQuery,你可以使用,$('em').html('something else');

标签: javascript php jquery html dom


【解决方案1】:

虽然您指定了 jQuery,但您可以使用 JavaScript 来执行此操作,但是您使用 getElementsByTagName 错误。

getElementsByTagName返回一个数组,你必须指定数组中的哪一个需要编辑。该演示应该为您提供足够的信息。

您没有指定数组中的哪一个需要更改 (document.getElementsByTagName("em").innerHTML),因此没有任何更改。

<em>Hello</em>
<em>Hello</em>
<em>Hello</em>
<em>Hello</em>
<em>Hello</em>
<em>Hello</em>

document.getElementsByTagName("em")[0].innerHTML = "GAH!";

会回来

GAH! Hello Hello Hello Hello Hello

http://jsfiddle.net/0jqvn9v2/

【讨论】:

  • 我真的很喜欢这样做的想法,它非常简单直接,但是“Bicycle”这个词是通过 php foreach 循环动态加载的。
  • @thismethod 然后您将使用 AJAX (GET),然后在您的成功回调中只需 document.getElementsByTagName("em")[0].innerHTML = data。数据是从 AJAX 传回的数据。
  • 我不是 100% 确定我知道该怎么做。
  • @thismethod 你知道怎么做 ajax 部分吗?如果是这样,我可以为 PHP 部分做一个简单的例子(你必须稍微编辑一下)。
  • 我不得不说我在 php 部分做得更好。
【解决方案2】:

您可以使用带有回调函数的 html() 来迭代它们。替换文本使用replace() 方法

$('em').html(function(i, v) {
  return v.replace("Bicycle", "(something different)");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<em>Bicycle</em>

如果内容是具体的,那么,

$('em').html(function(i, v) {
  return v === "Bicycle" ? "(something different)": v;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<em>Bicycle</em>

【讨论】:

  • 即使“bicycle”这个词在遍历 php foreach 循环时动态加载,它还能工作吗?
  • 它不是静态的在页面上。
猜你喜欢
  • 2013-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 2019-11-06
相关资源
最近更新 更多