如何使用jquery更改label标签内的文本

时间:2015-11-04 11:01:53

标签: jquery

如何将文字Add Your Image更改为Change Your Image

<label class="control-label col-md-offset-4 col-md-4 btn green feedbackImg" style="text-align:center;">
    Add Your Image
    <input type="file" name="data[Feedback][img]" class="form-control hide single_img_btn" id="1" style="display: none;">  
</label>
$('.feedbackImg').text('Change Your Image');

但它改变了label如下:

<label class="control-label col-md-offset-4 col-md-4 btn green feedbackImg" style="text-align:center;">
    Add Your Image
</label>

这意味着它也删除了输入标签。除了文本之外,我怎么能保持一致呢?

3 个答案:

答案 0 :(得分:6)

如果您可以更改HTML,只需在span中包含您要更改的文字,以便更轻松地选择:

<label class="control-label col-md-offset-4 col-md-4 btn green feedbackImg" style="text-align:center;">
    <span>Add Your Image</span>
    <input type="file" name="data[Feedback][img]" class="form-control hide single_img_btn" id="1" style="display: none;">  
</label>
$('.feedbackImg span').text('Change Your Image');

如果您无法更改HTML,那么您需要修改JS代码以检索并修改textNode本身:

$('.feedbackImg').contents().first()[0].textContent = 'Change Your Image';

答案 1 :(得分:3)

您需要更新textNode

  1. contents() - 用于获取包含文字和评论的所有节点
  2. eq() - 获取第一个元素,即textNode(label)
  3. replaceWith() - 使用新文字更新文字内容
  4. <强> CODE:

    &#13;
    &#13;
    $('.feedbackImg').contents().eq(0).replaceWith('Change Your Image');
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <label class="control-label col-md-offset-4 col-md-4 btn green feedbackImg" style="text-align:center;">
      Add Your Image
      <input type="file" name="data[Feedback][img]" class="form-control hide single_img_btn" id="1" style="display: none;">
    </label>
    &#13;
    &#13;
    &#13;

    OR

    &#13;
    &#13;
    $('.feedbackImg').contents()[0].nodeValue = 'Change Your Image';
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <label class="control-label col-md-offset-4 col-md-4 btn green feedbackImg" style="text-align:center;">
      Add Your Image
      <input type="file" name="data[Feedback][img]" class="form-control hide single_img_btn" id="1" style="display: none;">
    </label>
    &#13;
    &#13;
    &#13;

答案 2 :(得分:0)

将html更改为此

$('.image_label').text('Change Your Image');

然后在jquery

- (void)setExtractedImageForTableView:(UIImage *)extractedImage {
self.documentImageView.image = nil;
self.documentImageView = nil;
self.documentImageView = [[UIImageView alloc] initWithImage:extractedImage];
self.documentImageView.contentMode = UIViewContentModeScaleAspectFit;
if (!CGSizeEqualToSize(extractedImage.size, CGSizeZero)) {
    self.documentImageView.frame = CGRectMake(CGRectGetMinX(self.view.frame), CGRectGetMinY(self.tableView.frame) + 70, CGRectGetWidth(self.view.frame), (extractedImage.size.height/extractedImage.size.width) * CGRectGetWidth(self.view.frame));
    self.segmentedView.frame = self.documentImage.frame;
    [self.segmentedView addSubview:self.segment];
    [self.segmentedView addSubview:self.documentImageView];
    [self.tableView.tableHeaderView addSubview:self.segment];
    [self.tableView.tableHeaderView addSubview:self.documentImageView];
    CGRect frame = self.documentImageView.frame;
    frame.size.height = self.documentImageView.frame.size.height + 100;
    self.tableView.frame = CGRectMake(0, 0, 400, 300);
    self.tableView.tableHeaderView.frame = frame;
}
}