简体   繁体   English

使用jQuery更改文本

[英]Change Text using jQuery

I have a script that is pulling in news from Yahoo on a certain subject. 我有一个脚本可以从某个主题的雅虎那里获取新闻。 It renders the title of the news feed like this: 它呈现新闻源的标题如下:

<div class="header-title">subject - Yahoo! News Search Results</div>

I would like to change this to read differently. 我想改变这个以不同的方式阅读。 Since this is inserted via JS I thought I could change this with jQuery. 由于这是通过JS插入的,我以为我可以用jQuery改变它。

I attempted this: 我试过这个:

$('.header-title').text('Subject News');

that did not work, I then attempted this: 那不起作用,我接着尝试了这个:

$('.header-title').empty();
$('.header-title').text('Subject News');

that also did not work. 那也行不通。

Both of the above methods look as if they had no effect on the text. 上述两种方法看起来好像对文本没有影响。

I am not sure what to do to remove the old text and replace with my text. 我不知道如何删除旧文本并替换为我的文本。

Note: All of my code is inside jQuery's Document Ready IE: 注意:我的所有代码都在jQuery的Document Ready IE中:

$(function(){ 
   //Code Here 
});    

Don't forget to put your code in DOM ready : 不要忘记将代码放入DOM中

$(function() {
    $(".header-title").text("Subject News");
});

Otherwise the code should work fine. 否则代码应该正常工作。

This solution assumes you have no access to the other script that creates the feed widget 此解决方案假定您无权访问创建Feed小部件的其他脚本

WIthout knowing more about other script it sounds like it is asynchronous, and creates the title elements also. 在不了解其他脚本的情况下,它听起来像是异步的,并且还创建了标题元素。 You could have a timed interval loop that checks for the element to exist and once it exists do the update: 你可以有一个定时间隔循环来检查元素是否存在,一旦它存在就进行更新:

function changeYahooTitle(){
     var $yhooTitle=$('.header-title');
    if($yhooTitle.length){
        $yhooTitle.text('My New Title');
    }else{
        /* doesn't exist so check again in 1/10th second, will loop recursively until found*/
        setTimeout(changeYahooTitle, 100);
    }
}

Then on page load: 然后在页面加载:

$(function(){
  changeYahooTitle()

})

If you do have access to the other script it can be modified to accommodate your needs, or you can still use this solution 如果您确实可以访问其他脚本,则可以对其进行修改以满足您的需求,或者您仍然可以使用此解决方案

Try using html() instead of empty() and text() 尝试使用html()而不是empty()text()

$(document).ready(function(){
    $(".header-title").html("Subject News");
});

What's probably happening: 可能发生了什么:

  1. First you're setting the text: $('.header-title').text('Subject News'); 首先,您要设置文本: $('.header-title').text('Subject News');
  2. THEN ... after the data finishes the load of the Yahoo or whatever content your text gets replaced actually with the new fetched data 然后......在数据完成后,雅虎的负载或文本被替换的任何内容实际上都是新获取的数据

    Change the text inside the load callback (after data is loaded) and it will work. 更改加载回调内的文本(加载数据后),它将起作用。

It doesn't work because you call the function before the element is created. 它不起作用,因为您在创建元素之前调用该函数。 If you put your function inside <script> tag after the <div> element it should work 如果你将函数放在<div>元素之后的<div> <script>标记内,它应该可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM