jquery用空格寻址id

时间:2018-03-09 16:19:00

标签: jquery

我有一个问题是修改发送到iFrame的块,所以我尝试使用jQuery在documentReady之后修改它,我的问题是div的id包含空格:我想更改文本并添加一个图像

我怎么能用jQuery做到这一点?

<div id="Cash On Delivery" data-pm="Cash On Delivery">
 <div class="nameRow">
  <div class="methodIcon"> /* here I want to place a pic */</div>
  <div class="methodName">Cash On Delivery</div> <!--this I want to change-->
   <div class="paymentMethodDetails right">
   <div class="checkMark"></div>
 </div>
</div>

2 个答案:

答案 0 :(得分:2)

id值不得包含空格。它们差不多the only limitation

虽然可能能够使用属性选择器访问该元素:

var methodIcon = $("[id='Cash On Delivery'] .methodIcon");
var methodName = $("[id='Cash On Delivery'] .methodName");

示例:

var methodIcon = $("[id='Cash On Delivery'] .methodIcon");
var methodName = $("[id='Cash On Delivery'] .methodName");
methodIcon.text("pic goes here");
methodName.text("new method name here");
<div id="Cash On Delivery" data-pm="Cash On Delivery">
 <div class="nameRow">
  <div class="methodIcon"></div>
  <div class="methodName">Cash On Delivery</div> <!--this I want to change-->
   <div class="paymentMethodDetails right">
   <div class="checkMark"></div>
 </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

....坚持使用有效的id会更好。

答案 1 :(得分:1)

id不应该有空格,但是如果你想在jquery中定位那个id,请使用:

$("div[id='Cash On Delivery']");