使用jQuery添加占位符属性 [英] Adding placeholder attribute using Jquery

查看:191
本文介绍了使用jQuery添加占位符属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题.我只想在输入上添加一个占位符.这是代码:

I have a simple problem. I just want to add a placeholder on an input. Here's the code:

HTML:
<input type="text" name="link" class="hidden">

JQuery:
var link = document.querySelector('.link');
    link.style.display='block';
    link.classList.remove('hidden');
    if(val === 1)  // for a movie
        link.classList.add('placeholder="Add Movie Title. ."');
    else  // for an external link
        link.classList.add('placeholder="Add External Link . ."');

如您所见,由于语法错误,占位符不起作用.我搜索了有关问题的相关解决方案,但他们使用了类似的方法

As you can see, the placeholder doesn't work because the syntax is wrong. I searched related solutions to my problem but they use a different method like this

$("").attr("placeholder", "Type here to search");});

但是我需要的东西与我添加样式时使用的东西相似,但是我不确定是否还有其他方法可以做到.有类似的方法吗?

but I need something that is similar to what I used with adding style, but I'm not sure if there's other way to do it. Is there a similar way?

推荐答案

您只需要这个:

$(".hidden").attr("placeholder", "Type here to search");

classList用于操作类而不是属性.

classList is used for manipulating classes and not attributes.

这篇关于使用jQuery添加占位符属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆