Javascript 尝试根据屏幕大小预先添加文本

Javascript 尝试根据屏幕大小预先添加文本,javascript,jquery,html,Javascript,Jquery,Html,我试图替换元素上的文本,然后在其前面添加文本,但似乎无法使其正常工作。我让替换工作,然后当我试图准备文本时,它似乎停止了 HTML: 有问题吗?打电话给我们:1.555.55.5555 JS: 函数replacePhone(){ 如果($(窗口).width()786){ console.log($(“#topbar p”).text(); var text=$(“#topbar p”).prepend.(“有问题吗”); $(“#顶栏p”).text(text) } } $(窗口)。调整大

我试图替换元素上的文本,然后在其前面添加文本,但似乎无法使其正常工作。我让替换工作,然后当我试图准备文本时,它似乎停止了

HTML:


有问题吗?打电话给我们:1.555.55.5555

JS:

函数replacePhone(){
如果($(窗口).width()<786){
console.log($(“#topbar p”).text();
var text=$(“#topbar p”).text().replace('Have Questions?','');
$(“#顶栏p”).text(text)
}
如果($(窗口).width()>786){
console.log($(“#topbar p”).text();
var text=$(“#topbar p”).prepend.(“有问题吗”);
$(“#顶栏p”).text(text)
}
}
$(窗口)。调整大小(函数(){
replacePhone();
});
$(文档).ready(函数(){
replacePhone();
});
如果这对你来说更容易的话,这里有一把小提琴: 使用CSS媒体查询:

@介质(最小宽度:786px){
#topbar p:之前{
内容:“有问题吗?”;
}
}

这可以用于媒体查询吗@是的,这正是我所需要的。谢谢你的帮助和帮助!
<div id="topbar">
<p>Have Questions? Call us at: <b>1.555.55.5555</b></p>
<a href="#">Home</a>
<a href="#">Login</a>
</div>
function replacePhone() {
if ($(window).width() < 786) {
    console.log($("#topbar p").text());
    var text = $("#topbar p").text().replace('Have Questions?', '');
    $("#topbar p").text(text)
}
if ($(window).width() > 786) {
    console.log($("#topbar p").text());
    var text = $("#topbar p").prepend.('Have Questions?');
    $("#topbar p").text(text)
}
}
$(window).resize(function () {
replacePhone();
});
$(document).ready(function () {
replacePhone();
});